deoanam Posted May 10, 2007 Share Posted May 10, 2007 Hello everyone! I'm a newb here, and I've stumbled upon a puzzle that my meager PHP skills haven't been able to crack. It seems simple enough, though... any hints would be very much appreciated. I have some software (Wordpress, actually) that is sending HTML code of an unordered list when I call a particular function. I want to make that list into a multidimensional array. So something like this: <li>1</li> <li>2</li> <ul> <li>2.1</li> <li>2.2</li> </ul> <li>3</li> Will turn into this: [0] => 1 [1] => 2 [3] => array [0] => 2.1 [1] => 2.2 [4]=>3 Well, you get the idea... I'm trying to avoid a solution where I have to go through the string character by character and perform complex analyses on them. This is actually going to be the basis of a drop-down menu navigation system and so I can't have it taking up too much processing overhead. Any suggestions? Thanks so much! All the best, Matt Quote Link to comment https://forums.phpfreaks.com/topic/50742-reading-html-unordered-list-into-multidimensional-array/ Share on other sites More sharing options...
btherl Posted May 10, 2007 Share Posted May 10, 2007 If the html format is stable and simple (as it appears to be), you can get away with something like this for each line: if (preg_match('|<li>([^<]+)</li>|', $line, &$matches) === 1) { print "Matched {$matches[1]}\n"; } While preg_match() may not be as fast as using low-level string manipulation, it's still quite fast, it's simpler and more flexible. The other option is doing full HTML parsing, but I don't think it's necessary in your situation. Quote Link to comment https://forums.phpfreaks.com/topic/50742-reading-html-unordered-list-into-multidimensional-array/#findComment-249470 Share on other sites More sharing options...
deoanam Posted May 10, 2007 Author Share Posted May 10, 2007 Thanks for your response. Unfortunately, perhaps I don't know what to do with what you gave me because I couldn't seem to get it to do anything helpful However, I realized that I made a mistake in articulating what I was looking for... For this unordered list: <li>1</li> <li>2 <ul> <li>2.1</li> <li>2.2 <ul> <li>2.2.1</li> </ul> </li> </ul> </li> <li>3</li> Instead of this: [0] => 1 [1] => 2 [3] => array [0] => 2.1 [1] => 2.2 [2] => array [0] => 2.2.1 [4]=>3 I really wanted something like this: [0] => 1 [1] => 2 [3] => array [0] => 2.1 [1] => 2.2 array [0] => 2.2.1 [4]=>3 But you'll notice that on key [1], I have stupidly defined the value as a number AND an array (which is naughty... well, impossible, really). So I had an idea - what if I used the array's keys to contain the data. That way I could get something like this: [<li>1</li>] => [<li>2</li>] => array [<li>2.1</li>] => [<li>2.2</li>] => array [<li>2.2.1</li>] [<li>3</li>]=> So I tried this: $navlist0 = wp_list_pages('title_li=&sort_column=menu_order&echo=0&depth=3'); $delimiter = "\n"; $navlist1 = explode($delimiter, $navlist0); array_pop($navlist1); $navlist2 = array(); $number_of_keys = count($navlist1); foreach ($navlist1 as $key => $value) { $navlist1[$key] = trim($value); } for ($i = 0; $i < $number_of_keys; $i++) { $value_1 = $navlist1[$i]; //SECOND LEVEL if ( substr($navlist1[$i], 0, 3) == '<ul' ){ $i++; while (substr($navlist1[$i], 0, 4) != '</ul' ) { //THIRD LEVEL $value_2 = $navlist1[$i]; if ( substr($navlist1[$i], 0, 3) == '<ul' ){ $i++; while (substr($navlist1[$i], 0, 4) != '</ul' ) { $value_3 = $navlist1[$i]; echo "3"; $navlist2[$value_1][$value_2][$value_3] = NULL; $i++; } $i+2; } ////////////// echo "2"; $navlist2[$value_1][$value_2] = NULL; $i++; } $i+2; } ///////////////// echo "1"; $navlist2[$value_1] = NULL; } echo "<br />VALUE 3: $value_3<br />"; print_r($navlist1); echo "This is navlist:" . chr(13); print_r($navlist2); But it dinna work. It seems to only output the first level and dispenses with the rest. Anybody see anything incredibly obvious to prove my stupidity? This would be the one time I'd be grateful for such a proof. Cheers, Matt Quote Link to comment https://forums.phpfreaks.com/topic/50742-reading-html-unordered-list-into-multidimensional-array/#findComment-250101 Share on other sites More sharing options...
deoanam Posted May 16, 2007 Author Share Posted May 16, 2007 The answer to this question is here: http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html Quote Link to comment https://forums.phpfreaks.com/topic/50742-reading-html-unordered-list-into-multidimensional-array/#findComment-254076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.