Hello, I've been puzzling for half a day, I can't connect all 2 arrays of a parent and a child. there are arrays

Parent

[0] => Array ( [id] => 15 [title] => Название ) 

Child

  [0] => Array ( [id] => 1 [parent] => 15 ) [1] => Array ( [id] => 2 [parent] => 15 ) 

How to add the [child] key with the contents of the $ child array to the $ parent array, taking into account that $ parent ['id'] should be equal to $ child ['parent']

From this example, it should be so

  [0] => Array ( [id] => 15 [title] => Название [child] => Array ( [0] => Array ( [id] => 1 [parent] => 15 ) [1] => Array ( [id] => 2 [parent] => 15 ) ) ) 
  • I did not quite understand what should happen in the end :) Can you give an example, directly on the data from the question? - Nikolai Kim
  • Do you need to be in a structure (ie, initially set this way) or programmatically (so that after executing the code from 2 arrays, the necessary array is obtained)? - IVsevolod
  • Added by:) - dogmar
  • and what does not suit $ parent [0] ['child'] = $ child? - zb '
  • @eicto, using $ parent [0] ['child'] = $ child you do not guarantee that only elements whose parent is a given element will be added to the child parent. Besides, if you need to implement this scheme for several parents? After all, parent is specified as an array, which means that besides the first element there can be a second one. - BOPOH

2 answers 2

 foreach ($parent as &$parentElement) { $parentElement['child'] = array(); foreach ($child as &$childElement) { if ($childElement['parent'] != $parentElement['id']) { continue; } $parentElement['child'][] = $childElement; } } 

Solves the problem, although I do not like here that we are driving the child array back and forth.

You can do differently:

 $result = array(); foreach ($parent as &$parentElement) { $parentId = $parentElement['id']; $result["$parentId"] = array( 'parent' => $parentElement, 'child' => array(), ); } foreach ($child as &$childElement) { $parentId = $childElement['parent']; if (!isset($result["$parentId"])) { continue; } $result["$parentId"]['child'][] = $childElement; } 

Thus, we obtain an array consisting of a set of elements (parent and child array). It is not so convenient to use it, but we make far fewer passes during construction.

If necessary, you can get the source array from the last example. But this again - extra gestures.

And yet - the goal was to build an associative array, so I use "$ parentId" in the keys. It will work or not - I don’t know, it’s laziness to check, although in theory it should work out normally, not like with numbers.

  • Your second option works correctly, except for the fact that the Child key is created to a higher level, and the Child key should be created next to id, please look at the title of my post, where the original picture should turn out. - dogmar
  • Changed the penultimate construction to $ result ["$ parentId"] ['products_id'] ['child'] [] = $ childElement; Thank you very much. - dogmar
  • So the point is that in the second version, not exactly the array is created, which is needed, but it is created (with larger sizes of the original arrays) much faster than in the first variant. Comrade @ReinRaus, by the way, was a little ahead of me in this. His version and my first option - in fact there is one and the same. The difference is that I use pointers. Converting my second version to your desired form is easy: $ newResult = array (); foreach ($ result as $ values) {$ parent = $ values ​​['parent']; $ parent ['child'] = $ values ​​['child'] $ newResult [] = $ parent; } - BOPOH
  • But I don't like the fact that we are going through the cycle again. If the elements will be small - waste of time. It is better to use the first option. Or to alter a little logic - you can create two methods - getParent and getChild, and when you loop through the array received in the second variant, call them. This option seems more correct. Do not dwell on the name of the keys. - BOPOH
 $parent['child']=Array(); foreach ($child as $k=>$v) if ($v['parent']==$parent['id']) $parent['child'][]=$v; 
  • Can I have the full version? After all, your construction needs to be wrapped in a loop, because where do you get $ parent ['id']? - dogmar
  • I thought you needed an algorithm, but it turned out you need a snippet. Sorry. - ReinRaus
  • If I choose between the algorithm and the snippet, of course I will choose a snippet, I don’t have time at work :) I’d be at home .. I’ll go back to your lines of the algorithm :) - dogmar