www.webdeveloper.com
Results 1 to 8 of 8

Thread: Lost value of array after if compare

  1. #1
    Join Date
    Apr 2014
    Posts
    12

    Lost value of array after if compare

    I have this code
    Code:
    foreach ($result2 as  $rodic1 => $rodic_hodnot1)
                {           
                 foreach ($etc_uloziste as  $id => $etc_id)
                        {                           
                                        foreach ($etc_id as  $id2 => $etc_id2)
                                         {                                  
                                            //echo $etc_id2."\n";                                
                                            if($rodic1 ==$id)
                                               {
                                                 //echo $etc_id2."\n";                                              
                                                }                               
                                        }                                                                                                                                                                   
                        }
                }
    where the $etc_id2 is like library or room and $id is book
    [book] => Array
    (
    [0] => library
    [1] => room
    )
    But problem is when i //echo $etc_id2."\n" after if($rodic1 ==$id) i get only [0] => library.
    My question in here is why i cant get [1] => room . And how can i change mzı code to get it .
    Thank you for read my post and help

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    881
    Is there any way you could post the $result2 array object? Debugging your issue would be a lot easier if I had the data being used that seems to present the problem.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Apr 2014
    Posts
    12
    I have my $result2 in there http://pastebin.com/CMngg682 Sup3rkirby's
    Thanl you

  4. #4
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    881
    I'm sorry but it looks like I wasn't fully paying attention to your nested loops. It seems you have 2 arrays you are looping through: $result2 and $etc_uloziste. Could you share the $etc_uloziste array as well so I can compare these two arrays?
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  5. #5
    Join Date
    Apr 2014
    Posts
    12
    And this is my $etc_uloziste http://pastebin.com/M5dR4jsC

  6. #6
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    881
    Alright, bare in mind that I don't really understand the data structure you have in the sense that I have no idea what this data is for and what it means. Thus I'm not really sure what you are trying to achieve here or what the goal for this data is.

    From what I can tell though, there isn't anything 'wrong' with your code. This is just a matter of you need to loop through these arrays differently to get the result you want. Which goes back to the fact that I don't know exactly what you are trying to achieve so I can't really tell you what your code should look like.

    I can however make a suggestion that could help you see exactly what's going on in your code and thus help you tweak it to your needs.
    PHP Code:
    foreach($result2 as $rodic1 => $rodic_hodnot1) {           
        foreach(
    $etc_uloziste as $id => $etc_id) {                           
            foreach(
    $etc_id as $id2 => $etc_id2) {                                  
                echo 
    $rodic1 " == " $id " (" $etc_id2 ") <br />";
            }
        }

    Now to explain what this is and how it might help. You are comparing $rodic1 and $id in your if() statement before you echo out $etc_id2 on the page. If you run this, it will echo out every single if() statement so you can see exactly what the code compares, and in parenthesis it echoes out the value that would be shown on the page if the statement is true.

    Since you are expecting to see 2 values from part of your array but are only seeing one of them instead, running this will show you every value (including the ones you expect to see), but will show the if() statement being used to show or not show that value. So you should be able to look for '[1] => room' (in parenthesis) and find out which if() statement is being used. There you will be able to see why it doesn't match and thus why you don't see it on the page.


    Keep in mind this is just a way to help you debug your own problem. Because I know nothing about this data or what you are trying to achieve I can't be of much more help at the moment. If you could explain what it is you are trying to do then I could work out the issue myself and help you organize your loops.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  7. #7
    Join Date
    Apr 2014
    Posts
    12
    thank you it help me

  8. #8
    Join Date
    Apr 2014
    Posts
    12
    I think i have the problem to loop find something and if match and it dont loop more to the end

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles