I am trying to build an associative multidimensional array like this:
array(
[0] => Array(
Parent => Mr Smith,
Children => array(
Firstmane => Bob,
Surname => Smith,
Age => 16,
)
)
[1] => Array(
Parent => Mr Jones,
Children => array(
Firstmane => Davey,
Surname => Jones,
Age => 15,
)
)
)
My current code looks like this:
foreach($Parents as $parent) {
$opdata[] = ['Children'=>array(),'Parent'=>$name];
foreach($children as $child) {
$opdata['Children'][] = [
'Firstmane'=>$code,
'Surname'=>$cost,
'Age'=>$age,
];
}
}
However, This puts the ‘Children’ in a new array rather than in the child array of each parent. Like this:
array(
[0] => Array(
Parent => Mr Smith,
Children => array()
)
[1] => Array(
Parent => Mr Jones,
Children => array()
)
Children => array(
[0] => array(
Firstmane => Bob,
Surname => Smith,
Age => 16,
[1] => array(
Firstmane => Davey,
Surname => Jones,
Age => 15,
)
)
)
I can’t work how to add the child arrays to the correct nested array with the correct parent.
Childrenkey to the pushed array you create in the 1st foreach, not to the main array where you miss the numerical index of the "parent".