开发者

Organizing results by category explanation

Can someone please explain what is going on in the code below? I understand it until the $categories[$category][] = $row['agency']; line.

$categories = array();
while ($row = mysqli_fetch_array($result))
{
    $category = $row['category'];
    $catego开发者_运维技巧ries[$category][] = $row['agency'];
}


The results you are getting from your database are likely:

Category           Agency

Security     Police
Security     CIA
Security     FBI
Government   Education
Government   Health
Misc         AnotherAgency

The $category = $row['Category']; gets the value from the result for the Category (i.e Security, Government, Misc).

After that the Categories array is added to. It is an array of arrays. So it stores for each category an array of agencies.

array('Security'   => array('Police', 'CIA', 'FBI'),
      'Government' => array('Education', 'Health'),
      'Misc'       => array('AnotherAgency'));


$row['agency'] is a vector. It is getting "pushed" onto the vector `$categories[$category]'. It is equivalent to the line

array_push($categories[$category], $row['agency']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜