0

I have a code that fetches rows from my mysql and displays in a categorized format, but I couldn't understand the line $array[$rows['parent_id']][] = $rows; from the code. The code looks like:

<?php
    function display_menus_revised()
    {
        $sql = "SELECT * FROM categories";
        $query = mysql_query($sql) or die(mysql_error());
        $array = array();
        if (mysql_num_rows($query)) {
            while ($rows = mysql_fetch_array($query)) {
                $array[$rows['parent_id']][] = $rows;
            }
            loop_array($array);
        }
    }
?>

When the above code is executed the list of items are shown properly with their names but I don't understand what is the use of the extra [] in the line $array[$rows['parent_id']][]. Please do explain I am very much new to php.

Antony
  • 14,372
  • 10
  • 41
  • 71
  • Better you can print the array " print_r($array) ". then you can get some clear idea why they are stored data like that. – Ravichandran Jothi Jan 16 '14 at 06:15
  • The idea seems to be to collect the rows with the same parent. So you get an array of arrays. `array(parentid1 => array(row1, row2, ...), parentid2=> array(row3, row4, ...) ... )` – towr Jan 16 '14 at 06:16

0 Answers0