1

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?

5 Answers 5

3

The problem is that you redefine $result[$r['sname']] each time. You only need to define it if it is not already defined.

$result = array(); // initialize this to an appropriate type!
foreach($row as $r) 
{

   if(!isset($result[$r['sname']]))
   {
      $result[$r['sname']] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']));
      continue;
   }
   $result[$r['sname']]['divisions'][$r['did']] = $r['dname'];

}
Sign up to request clarification or add additional context in comments.

Comments

1
$result = array();
foreach($row as $r) 
{

     $result[$r['sname']]['id'] = $r['sid'];
     $result[$r['sname']]['divisions'][$r['did']] = $r['dname'];

}

1 Comment

+1 Assuming it works, this is a pretty elegant read. Of course it re-initializes the 'id' value each time, but that's not so expensive.
0

Check for existence of the key with isset() or array_key_exists() beforehand so that it doesn't get rewritten.

Comments

0

i think you want:

$result[$r['sname']][] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']));

Comments

0
$result = array();
foreach($row as $r) 
{
    if(isset($result[$r['sname']]))
    {
        array_push($result[$r['sname']], array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname'])));
    }
    else
    {
        $result[$r['sname']] = array('id' => $r['sid'], 'divisions' => array($r['did'] => $r['dname']))
    }
}

What you are currently doing is overwriting the entry for $result[$r['sname']], so it will only ever have one array in it.

I use the if because $array[] = is quicker than array_push see php docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.