Jump to content

From unordered list to multidimensional array?


Roland_D

Recommended Posts

Hello guys I've been trying to solve this for a while now I have this:

 

<ul id="0" class="nestedsortable">
<li id="29">
   <div class="itemrow">Home</div>
    <ul id="29"> </ul>
   </li>
   <li id="37">
   <div class="itemrow">Page 1</div>
 <ul id="37">
        <li id="30">
     <div class="itemrow">Page 3</div>
   <ul id="30"></ul>
     </li>
       </ul>
   </li>
   <li id="36">
   <div class="itemrow">Page 2</div>
 <ul id="36"></ul>
</li>
   <li id="31">
   <div class="itemrow">Page 4</div>
 <ul id="31"></ul>
</li>
</ul>

 

 

And I'd like to get this:

 

$item[0][0]=29;
$item[0][1]=37;
$item[0][2]=36;
$item[0][3]=31;
$item[37][0]=30;

 

So the bottom line here is I have a nested unordered list that is treated almost like an XML... now I need to collect the ID's of each LI in an ordered fashion and group them by UL ID's...

 

Now, does it make any sense? Probably the fact that is nested has nothing to do with the process cause each UL has it's own ID but the UL's are contained into the LI's...

 

Any help is appreciated! Thanks!

Recursively,

function buildArrayFromLists($parent, &$item) {
    for each $ul directly under the $parent {
        $item[id from $ul] = array();
        for each $li directly under $ul {
            $item[id from $ul][] = id from $li;
            buildArrayFromLists($li, $item);
        }
        // optionally
        if $item[id from $ul] is empty then unset($item[id from $ul]);
    }
}

$xml = '
<ul id="0" class="nestedsortable">
    <li id="29">
        <div class="itemrow">Home</div>
        <ul id="29"></ul>
    </li>
    <li id="37">
        <div class="itemrow">Page 1</div>
        <ul id="37">
            <li id="30">
                <div class="itemrow">Page 3</div>
                <ul id="30"></ul>
            </li>
        </ul>
    </li>
    <li id="36">
        <div class="itemrow">Page 2</div>
        <ul id="36"></ul>
    </li>
    <li id="31">
        <div class="itemrow">Page 4</div>
        <ul id="31"></ul>
    </li>
</ul>';




$array = array();
$id    = null;




// Iterate the array with some conditions 
function iterate($a)
{
    global $array;
    
    foreach ($a as $k) {
        if (is_array($k)) {
            if (array_key_exists('tag', $k) && $k['tag'] == 'UL') {
                if (array_key_exists('level', $k)) {
                    if (array_key_exists('attributes', $k) && array_key_exists('ID', $k['attributes'])) {
                        if ($k['level'] == 3) { // 3 represent <ul id="29"> || <ul id="37"> || <ul id="36"> || <ul id="31">
                            $array[0][] = $k['attributes']['ID'];
                            $id         = $k['attributes']['ID'];
                        }
                        if ($k['level'] == 5) { // 5 represent <ul id="30">
                            $array[$id][] = $k['attributes']['ID'];
                        }
                    }
                }
            }
            iterate($k);
        }
    }
    return $array;
}




// Parse XML data into an array structure - http://php.net/manua...into-struct.php
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml, $values, $index);
xml_parser_free($parser);




$item = iterate($values);




// echo '<pre>';    
// print_r($values);
// echo '<pre>';




echo '<pre>';
print_r($item);




/**
Result:




Array
(
    [0] => Array
    (
        [0] => 29
        [1] => 37
        [2] => 36
        [3] => 31
    )




    [37] => Array
    (
        [0] => 30
    )
)
*/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.