I experience difficulty in order to sort through arrays. Google a few hours away - surprisingly, did not find anything.

The essence is as follows. There are several arrays of the form:

Array ( [2] => Array ( [n] => 1 [d] => 1 [p] => ) [3] => Array ( [n] => 2 [d] => 2 [p] => ) [4] => Array ( [n] => 3 [d] => 3 [p] => ) [5] => Array ( [n] => 4 [d] => 4 [p] => ) [6] => Array ( [n] => 5 [d] => 5 [p] => ) Array ( [2] => Array ( [n] => 1 [d] => 1 [p] => ) [3] => Array ( [n] => 2 [d] => 2 [p] => ) [4] => Array ( [n] => 3 [d] => 3 [p] => ) [5] => Array ( [n] => 4 [d] => 4 [p] => ) [6] => Array ( [n] => 5 [d] => 5 [p] => ) Array ( [2] => Array ( [n] => 1 [d] => 1 [p] => ) [3] => Array ( [n] => 2 [d] => 2 [p] => ) [4] => Array ( [n] => 3 [d] => 3 [p] => ) [5] => Array ( [n] => 4 [d] => 4 [p] => ) [6] => Array ( [n] => 5 [d] => 5 [p] => ) 

The key [n] is id.

We need to go through all the variations of id. Get all possible iterations.

1-1-1 1-2-1 1-3-1 1-1-2 1-1-3 1-1-4 2-2-2 ... In other words, to create such an image a maximum of bulkheads, so that later you can already work with other keys, compare them, and generally use :)

I will be glad to any hint :) Thank you!

    2 answers 2

    The question is not very clear, I understood this:

     $ids[1]=array_filter($arr1, function($v, $k) { return $v['n']; }); // создаем массивы из id $ids[2]=array_filter($arr2, function($v, $k) { return $v['n']; }); $ids[3]=array_filter($arr3, function($v, $k) { return $v['n']; }); foreach($ids[1] as $v1){ foreach($ids[2] as $v2){ foreach($ids[3] as $v3){ $out[$v1.'-'.$v2.'-'.$v3]=1; } } } echo implode('<br>',array_keys($out)); 

      Preparing the whole array, as suggested by Nsk - well. But here's the best option if you run into a memory size limit. Or if you need to stop without going over everything. Please love and favor: yield

       // Исходные данные $arr1 = [ ['n' => 1], ['n' => 2], ['n' => 3] ]; $arr2 = [ ['n' => 1], ['n' => 2], ['n' => 3] ]; $arr3 = [ ['n' => 1], ['n' => 2], ['n' => 3] ]; // Получение очередного ключа function getKey($arr) { foreach($arr as $v) { // используйте это! yield $v['n']; } } // Получение составного ключа function getCompositeKey($arr1, $arr2, $arr3) { foreach(getKey($arr1) as $v1){ foreach(getKey($arr2) as $v2){ foreach(getKey($arr3) as $v3){ yield sprintf('%d-%d-%d', $v1, $v2, $v3); } } } } foreach(getCompositeKey($arr1, $arr2, $arr3) as $val) { echo $val . PHP_EOL; }