0

I have the following code. In the last foreach I'd like to add $last_array and $last2_array as key/value pairs inside $display array. Here's what I tried:

$display[] = array($last_array => $last2_array); //doesn't work
print_r($display); //under the loop prints nothing

The code:

$display=array();//declare the array outside the loop
foreach ($array as $arrays){ 
    foreach ($arrays as $elem) {
        unset($elem['id']); //Removes id key
        unset($elem['idno']); //Removes idno key
        foreach ($elem as $last_array => $last2_array) {
            //code here                       
            #echo $last_array. ": ".$last2_array."<br>";//This prints data, it's not empty.
        }       
        echo "<br>";
    }
}

Thanks in advance.

6
  • If you want to edit an array while looping with foreach, use &: foreach ($array as &$arrays){ Commented Jun 17, 2013 at 22:27
  • your array $display prints blank so how will it work anyway? how are you constructing the array $display? Commented Jun 17, 2013 at 22:35
  • @amigura: I tried with this code inside the last loop: $display[] = array($last_array => $last2_array); This doesn't work Commented Jun 17, 2013 at 22:38
  • just a guess. $display=array(1,2,3); you want to add 4 and 5 array_push($display, 4, 5); array push Commented Jun 17, 2013 at 22:42
  • what is $array data? Commented Jun 17, 2013 at 22:51

2 Answers 2

1
$display=array();//declare the array outside the loop
foreach ($array as $arrays){ 
    foreach ($arrays as $elem) {
        unset($elem['id']); //Removes id key
        unset($elem['idno']); //Removes idno key
        foreach ($elem as $last_array => $last2_array) {
          $display[$last_array] = $last2_array;
        }       
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I think this will work! I'm having a strange issue, if I add your line of code I get this error from apache: Invalid argument supplied for foreach() in /var/www/search2.php on line 33. Line 33 Is the first foreach loop. If I replace your line with echo $last_array.$last2_array."<br>";. It prints data just fine with no errors. Guess I'll have to do some debugging!
It just got weirder... In order for your line to work I have to let the line that echoes the results, otherwise I get the error above and $display appears empty. I if leave the echo line in place print_r(display); shows only the last element from $last2_array. Array ( [object_id] => 549 [display_label] => test [ca_objects.description] => [ca_objects.type_id] => Audio )
maybe one of these is not an array but an object: $array, $arrays, $elem
This code gathers all elements: $display[] = array($last_array => $last2_array); But not in the way I want, it makes an array of each value. A print_r($display) portion: Array ( [0] => Array ( [object_id] => 13 ) [1] => Array ( [display_label] => La Bohème / PUCCINI - 2010 ) [2] => Array ( [ca_objects.description]. It's late here, guess I'll find it out after a good night sleep! Thanks for pointing me in the right direction.
0

i recommend you do a search on Multidimensional Arrays to learn abit more on creating and accessing them.

$object_id=$array['results'][0]['object_id'];
$display_label=$array['results'][0]['display_label'];

$display = array($object_id => $display_label);
print_r($display);

1 Comment

I like that... I'll check the code later, thanks! I've been doing some serious reading but I'm too tired now, thanks again! I'll let you know the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.