Hello,
I have an array like so:
sid sname did dname
1 Basketball 1 Mini
1 Basketball 3 Cadet
2 Baseball 8 Little League
2 Baseball 6 Junior League
1 Basketball 5 Masters
I was trying to get this and transform it to a nested array like so:
array('Basketball' => array(
'id' => 1,
'divisions' => array(
1 => 'Mini',
3 => 'Cadet',
5 => 'Masters'
)
),
'Baseball' => array(
'id' => 2,
'divisions' => array(
8 => 'Little League',
6 => 'Junior League'
)
)
);
And I am using this foreach loop which is not working, it replaces each division entry so I'm left with only one division entry which is the last entry.
$result = '';
foreach($row as $r)
{
$result[$r['sname']] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']));
}
This foreach loop gives me this result:
array('Basketball' => array(
'id' => 1,
'divisions' => array(
5 => 'Masters'
)
),
'Baseball' => array(
'id' => 2,
'divisions' => array(
6 => 'Junior League'
)
)
);
I don't understand what is wrong here.. can anybody help me out here?