Jump to content

Recommended Posts

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

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.

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.