skip to Main Content

I have an array (representing $detective) that looks like this:

Array
(
    [detectiveid] => 52
    [name] => Waal - Noord
    [rise_value] => 2
    [rise_time] => 3
    [drop_value] => 2
    [drop_time] => 3
    [max_time] => 80
    [max_speed] => 30
    [max_range] => 1000
    [min_hits] => 2
    [virtuals] => Array
        (
            [103377] => Array
                (
                    [virtualid] => 103377
                    [latitude] => 51.8683
                    [longitude] => 5.88432
                    [template] => enose
                    [peaks] => Array
                        (
                            [1697893319] => Array
                                (
                                    [total] => 1.97
                                    [ignore] => 
                                    [datetime] => 2023-10-21 15:01:59
                                    [timestamp] => 1697893319
                                )

                        )

                )

            [103378] => Array
                (
                    [virtualid] => 103378
                    [latitude] => 51.8544
                    [longitude] => 5.86296
                    [template] => enose
                    [peaks] => Array
                        (
                        )

                )

            [103379] => Array
                (
                    [virtualid] => 103379
                    [latitude] => 51.8765
                    [longitude] => 5.81837
                    [template] => enose
                    [peaks] => Array
                        (
                            [1697895022] => Array
                                (
                                    [timestamp] => 1697895022
                                    [datetime] => 2023-10-21 15:30:22
                                    [total] => 1.83
                                    [ignore] => 1
                                )

                            [1697895083] => Array
                                (
                                    [timestamp] => 1697895083
                                    [datetime] => 2023-10-21 15:31:23
                                    [total] => 2.55
                                    [ignore] => 
                                )

                        )

                )

            [103380] => Array
                (
                    [virtualid] => 103380
                    [latitude] => 51.8825
                    [longitude] => 5.78394
                    [template] => enose
                    [peaks] => Array
                        (
                        )

                )

        )

)

But at a certain point I had unexpected behaviour while using foreach ($detective['virtuals'] as $virtual). Then I tried to use foreach ($detective['virtuals'] as $key => $value) and got the expected result.

Using foreach ($detective['virtuals'] as $virtual) and echo $virtual['virtualid'] gives me this unexpected result:

103377
103378
103379
103379

Using foreach ($detective['virtuals'] as $key => $value) and echo $detective['virtuals'][$key]['virtualid'] gives me this unexpected result:

103377
103378
103379
103380

Anyone who has an explanation for this? These results I get when I replace the upper method with the lower method, if I first run the upper method and then run the lower method also the lower method is echoing the upper result. It kind of looks like the upper method mutilates the array by not only printing it but also manipulating it.

2

Answers


  1. Chosen as BEST ANSWER

    Your first option returns:

    foreach ($detective['virtuals'] as $key => $virtual){
        echo "key: ".$key;
        echo  ", ";
        echo "virtualid: ".$detective['virtuals'][$key]['virtualid'];
        echo  "<br />";
     }
    
    key: 103377, virtualid: 103377
    key: 103378, virtualid: 103378
    key: 103379, virtualid: 103379
    key: 103380, virtualid: 103379
    

    Your seconds option returns:

    foreach ($detective['virtuals'] as $virtual){
        echo "key: ".key($detective['virtuals']);
        echo  ", ";
        echo "virtualid: ".$virtual['virtualid'];
        echo  "<br />";
    }
    
    key: 103377, virtualid: 103377
    key: 103377, virtualid: 103378
    key: 103377, virtualid: 103379
    key: 103377, virtualid: 103379
    

    And if you count the print_r result I initially posted as the third option, that gives me the expected result:

    print_r($detective);
    
    key: 103377, virtualid: 103377
    key: 103378, virtualid: 103378
    key: 103379, virtualid: 103379
    key: 103380, virtualid: 103380
    

    I agree with your conclusion that the data in the array is incorrect, but is, or can that be my fault when getting these three not convergent outputs from the same array? I have been programming ANSI C in the past and this looks like a memory issue to me but I was hoping to find a fix for a mistake that I made.

    Only reason I can think about now is to solve it by not using an associative array so getting rid of the keys and only using virtualid as the identifier. What I kind of already was doing but I get the 103379 two times instead of 103379 followed by 103380 like the print_r is showing me.


  2. The only conclusion I can come to is that the data in your array is incorrect.

    In the first example

     foreach ($detective['virtuals'] as $virtual)
    

    You must be using $virtual['virtualid']. In the second case you are using the array’s actual key and not the value stored in the data.

    In that if you follow my logic $virtual['virtualid'] Must be wrong.

    You can check this by outputting both

    foreach ($detective['virtuals'] as $key => $virtual){
        echo "key: ".$key;
        echo  "n";
        echo "virtualid: ".$virtual['virtualid'];
        echo  "------------------------n";
     }
    

    Or ( if you prefer not to use $key => )

    foreach ($detective['virtuals'] as $virtual){
        echo "key: ".key($detective['virtuals']);
        echo  "n";
        echo "virtualid: ".$virtual['virtualid'];
        echo  "------------------------n";
     }
     
    

    That’s the only thing that makes sense. It’s worth checking when you are relying on the key further along ( maintaining it in two places ). You could have errors in the future if there is an issue before this code, like when the array is built.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search