-1

Guys i've got an array thats like this:

  array(3) {
    [2]=>
    array(1) {
      ["name"]=>
      array(2) {
        [0]=>
        string(13) "row1"
        [1]=>
        string(13) "row3"
      }
    }
    [5]=>
    array(1) {
      ["name"]=>
      array(2) {
        [0]=>
        string(15) "row1"
        [1]=>
        string(15) "row3"
      }
    }
    [3]=>
    array(1) {
      ["name"]=>
      array(2) {
        [0]=>
        string(13) "row1"
        [1]=>
        string(13) "row3"
      }
    }

What i want to achieve is make foreach loop the 0 elements (row1) and then loop through 1 (row3) and go on like this. Is there a way to do that?

Anonymous
  • 748
  • 3
  • 10
  • 22

2 Answers2

1

You could try to rebuild the array:

$rows = array();
foreach($array as $subarray) 
  foreach($subarray as $key => $value)  
    $rows[$key][] = $value;

At this point al the same subelements from the array are together in a new array, and now you can easy loop over a subelement:

  foreach($rows as $key => $value)  
    echo 'processing row: ' . $key ' with value ' . $value;
JvdBerg
  • 21,777
  • 8
  • 38
  • 55
0

I found a different approach to this problem, the JvdBeg solution is working wonderful, but if someone is stuck in a similar situations, this is how i did it: $key = key($arr); $keys = array_keys($arr);

    for ($i=0;$i<sizeof($arr[$key]['index']);$i++) {
      for($k=0;$k<sizeof($arr);$k++) {
        $key = $keys[$k];
        echo "\n";
      }
    }
Anonymous
  • 748
  • 3
  • 10
  • 22