On 11 Jan 2007, at 15:52, Andy Armstrong wrote:
   "Complex arrays are sometimes rather copied than referenced. Thus
    following example will not work as expected." [1]

Although in fact this does work on both PHP 4.4.4 and PHP 5.2.0. Way to go with the alarmist documentation which means I still don't know whether it's right or just happens to work.

In fact if there's one thing I hate more than PHP itself right now it's the fact that its online documentation is rammed to the gills with cargo-culted half truths, suspicions and downright rubbish.

<?php

    $list = array(
        '1' => array(
            'name'      => 'Frib',
            'parent_id' => '3'
        ),
        '2' => array(
            'name'      => 'Frob',
            'parent_id' => '3'
        ),
        '3' => array(
            'name'      => 'Top 1',
            'parent_id' => null
        ),
        '4' => array(
            'name'      => 'Top 2',
            'parent_id' => null
        ),
        '5' => array(
            'name'      => 'Frub',
            'parent_id' => '4'
        ),
        '6' => array(
            'name'      => 'Freb',
            'parent_id' => '4'
        )
    );

    $root = array();

    foreach ($list as $id => $obj) {
        if (is_null($obj['parent_id'])) {
            $root[] =& $list[$id];
        } else {
            $list[$obj['parent_id']]['children'][] =& $list[$id];
        }
    }

    print_r($root);

?>


--
Andy Armstrong, hexten.net

Reply via email to