There is an array:

Array ( [title] => Array ( [0] => Mortal Kombat X [1] => Mortal Kombat 2 ) [desc] => Array ( [0] => 489 [1] => 32 ) [metatitle] => Array ( [0] => 34 [1] => 4 ) [date] => Array ( [0] => 17.02.2017 [1] => 17.02.2017 ) [magnet] => Array ( [0] => http://site1 [1] => http://site2 ) [torrent] => Array ( [0] => http://site1 [1] => http://site2 ) ) 

I need to make a cycle and pull out 1 value with a 0 index, then increment the counter and pass again. I know the for loop, but I can't figure it out with forex. No finite number of elements in the array is known here. Therefore, you need to start from scratch and output until it ends.

The algorithm is as follows: 1 cycle of passage should give the following result:

Mortal Kombat X | 489 | 34 | February 17, 2017 | http: // site1 | http: // site1

Then the counter increases and data under index 1 is drawn.

How to do this? I can not overcome yet that Forech.

  • so foreach not needed here, it is also possible for two passes. for($i = 0; $i < 2; ++$i) { echo $array['title'][$i] .', ' . $array['desc'][$i] . '<br/>'; } for($i = 0; $i < 2; ++$i) { echo $array['title'][$i] .', ' . $array['desc'][$i] . '<br/>'; } ...... if it is not known how many passes should be, then it suffices to take $count = count($array['title']); - Alexey Shimansky

1 answer 1

Most likely something like this:

 <?php $arr = array( 'title' => array('Mortal Kombat X', 'Mortal Kombat 2'), 'desc' => array(489, 32), 'metatitle' => array(34, 4), 'date' => array('17.02.2017', '17.02.2017'), 'magnet' => array('http://site1', 'http://site2'), 'torrent' => array('http://site1', 'http://site2') ); $newArr = array(); foreach($arr as $key => $val) { for($i = 0; $i < count($val); $i++) { $newArr[$i][$key] = $val[$i]; } } print_r($newArr); /* * Array ( * [0] => Array ( [title] => Mortal Kombat X [desc] => 489 [metatitle] => 34 [date] => 17.02.2017 [magnet] => http://site1 [torrent] => http://site1 ) * [1] => Array ( [title] => Mortal Kombat 2 [desc] => 32 [metatitle] => 4 [date] => 17.02.2017 [magnet] => http://site2 [torrent] => http://site2 ) * ) */ for($i = 0; $i < count($newArr); $i++) { echo implode(' | ', $newArr[$i]) . '<br/>'; } /* * Mortal Kombat X | 489 | 34 | 17.02.2017 | http://site1 | http://site1 * Mortal Kombat 2 | 32 | 4 | 17.02.2017 | http://site2 | http://site2 */ 
  • Misunderstood me. I already have an array. At the beginning of its structure. The finite number of elements is unknown, because it will always be different. Therefore, you need to take place with the help of Forech. - LEX
  • one
    @LEX, well, it goes through foreach , then it takes all the values ​​and inserts it into a new array, at the end there is the final array which turns out to do what you described as the “expected result” you just have to go through the loop for $newArr and spawn every subarray. - MedvedevDev
  • @MedvedevDev TS'u probably need something like this: sandbox - Edward
  • @Edward, we can only guess xDD - MedvedevDev
  • Exactly what is needed. And no need to guess :) I described everything in detail :) - LEX