There are two arrays:

Array ( [0] => 5 [1] => Wooow! [2] => Alex [3] => 4 [4] => Good [5] => Pete [6] => 2 [7] => Bad [8] => John [9] => 1 [10] => Shit [11] => Steve ) Array ( [0] => rating [1] => comment [2] => person [3] => rating [4] => comment [5] => person [6] => rating [7] => comment [8] => person [9] => rating [10] => comment [11] => person ) 

How do i get such an array?

 Array ( [0]=>Array( [rating]=>5 [comment]=>Wooow! [person]=>Alex ) [1]=>Array( [rating]=>4 [comment]=>Good [person]=>Pete ) [2]=>Array( [rating]=>2 [comment]=>Bad [person]=>John ) ... ) 

Thank.

    1 answer 1

    I'm not sure that you need the second array, but the host is the master:

     $a = Array ( 0 => 5, 1 => 'Wooow!', 2 => 'Alex', 3 => 4, 4 => 'Good', 5 => 'Pete', 6 => 2, 7 => 'Bad', 8 => 'John', 9 => 1, 10 => 'Shit', 11 => 'Steve' ); $b = Array ( 0 => 'rating', 1 => 'comment', 2 => 'person', 3 => 'rating', 4 => 'comment', 5 => 'person', 6 => 'rating', 7 => 'comment', 8 => 'person', 9 => 'rating', 10 => 'comment', 11 => 'person1' ); $count = 3; // сколько в дольке будет // Вариант 1 $b = array_slice($b, 0, $count); $c = array_map( function($v) use($b) {return array_combine($b, $v);}, array_chunk($a, $count) ); // Вариант 2 // Отличается от первого, что использует весь $b $c = array_chunk($a, $count); foreach ($c as &$v) { $v = array_combine(array_slice($b, 0, $count), $v); $b = array_slice($b, $count); } // Вывод var_dump($c); 
    • Thanks, I was able to implement a similar option with an explicit indication of the number of columns. It would be desirable without explicit instructions. The second array is not fundamentally the same, the first three values ​​are possible. - Eugene
    • one
      @ Eugene if in $b - 3 values, then $count = count($b); - without explicit instructions :) - Manitikyl
    • Thank you, I knew that everything is ugly simply). - Eugene
    • one
      @ Eugene, where did the number 3 come from? from the air? all the same, somewhere, the number 3 will have to be registered))) If you are talking about the unique fields of this array (and there are 3 of them), then $b = array_unique($b); . If so, you should have a plus sign ;) - Manitikyl
    • Thanks, I will try to solve. - Eugene