Two Dimensional Array with explode()

Hi,

I’d like to create a two dimensional array like the following

Array ( [0] => Array ( [0] => message1 [1] => author1 [2] => 2006-08-31 )
        [1] => Array ( [0] => message2 [1] => author2 [2] => 2004-05-01 )
        [2] => Array ( [0] => message3 [1] => author3 [2] => 2001-02-21 )
      ) 

from this variable

$data =  "[SEPARATOR_B]message1[SEPARATOR_A]author1[SEPARATOR_A]2006-08-31
          [SEPARATOR_B]message2[SEPARATOR_A]author2[SEPARATOR_A]2004-05-01
          [SEPARATOR_B]message3[SEPARATOR_A]author3[SEPARATOR_A]2001-02-21
          ";

Makeing it one dimensional with the explode(), it’s fine. But when I tried to make it two dimensional, I couldn’t get it worked. The code below does not work.

$data = explode("[SEPARATOR_B]", $data);
foreach ($data as $dat){
$data = explode("[SEPARATOR_A]", $dat);
}
print_r ($data);

Any information would be greatly appreciated.

You’re almost there


foreach ($data as $key => $dat){
   $data[$key] = explode("[SEPARATOR_A]", $dat);

You need to define the final array as an array…
Ok that is probably the worst explanation I have ever heard!
This works:


<?php
$data =  "[SEPARATOR_B]message1[SEPARATOR_A]author1[SEPARATOR_A]2006-08-31
          [SEPARATOR_B]message2[SEPARATOR_A]author2[SEPARATOR_A]2004-05-01
          [SEPARATOR_B]message3[SEPARATOR_A]author3[SEPARATOR_A]2001-02-21
          ";
		 $data = explode("[SEPARATOR_B]", $data);
		 echo '<pre>';
		 print_r($data);
		 echo '</pre>';
		 	$data2=array();
		foreach ($data as $dat){
			$data2[] = explode("[SEPARATOR_A]", $dat);
		}
		 echo '<pre>';
		 print_r($data2);
		 echo '</pre>';

?>

returns


Array
(
    [0] => Array
        (
            [0] =>
        )

    [1] => Array
        (
            [0] => message1
            [1] => author1
            [2] => 2006-08-31

        )

    [2] => Array
        (
            [0] => message2
            [1] => author2
            [2] => 2004-05-01

        )

    [3] => Array
        (
            [0] => message3
            [1] => author3
            [2] => 2001-02-21

        )

)



(sorry, brain not working yet…)

Spike

Thank you so much, stereofrog and Spike! I really appreciate it.