开发者

Organizing results by category explanation

开发者 https://www.devze.com 2023-03-28 08:23 出处:网络
Can someone please explain what is going on in the code below? I understand it until the $categories[$category][] = $row[\'agency\']; line.

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

精彩评论

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