Jump to content

Beginner: explode array into multidimensional array


mac_gabe

Recommended Posts

Hi, I'm trying to explode an array into a multidimensional array using <br /> tags as splitters but just can't get the syntax right! I can do lines of the array one by one, but not the whole lot at once. I'd really appreciate a quick example of an explode inside an array.

 

$myarray looks something like this with print_r:

(
    [0] => 
    [1] => some_html<br />some_html<br />some_html<br />
    [2] => some_html<br />some_html<br />some_html<br />
    [3] => some_html<br />some_html<br />some_html<br />
)

 

and I want to turn it into this:

(
    [0] => 
    [1] =>
    	[0]some_html
    	[1]some_html
    	[2]some_html
    [2] => 
    	[0]some_html
    	[1]some_html
    	[2]some_html
    [3] =>
    	[0]some_html
    	[1]some_html
    	[2]some_html
)

 

I've tried this

$myarray = explode ("<br />", $myarray);

but it doesn't work …

 

It doesn't matter too much if there's are a few empty lines in the array from the final <br /> tags.

 

Many thanks, as ever, for any help.

Link to comment
Share on other sites

OK I've done it with a for loop

$original_array_count= count($original_array);
for ($u=1; $u < $original_array_count; $u++) {
   $new_array[$u] = explode('<br />', $original_array[$u]);
}
print_r ($new_array);

 

I thought maybe there was a simpler way of doing it but I guess this works fine.

Link to comment
Share on other sites

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.