Alex Rivera | Logout

Store and retrieve a multidimensional array using php and mysql

Asked 2009-05-21T15:11:36.633
10

I have a multidimensional array in PHP like this:

$array = array(
    "Part1" => array(
        "Subpart1" => array(0, 1),
        "Subpart2" => array(1, 0)
    ),
    "Part2" => array(0),
    "Part3" => array(0, 1, 0)
);

Now I want to store this array in a MySQL table and retrieve it exactly like this again on another PHP page.

I've been trying using serialize() and unserialize()

$array= serialize($array);

and then on the other page

$array= $row['Array'];
$array2 = array();
$array2 = unserialize($array);

But I seem to do something wrong, in the beginning I got a var_dump of bool(false) and now I get a var_dump of NULL.

Edit
Report

1 Answer

1

You could use json encode, json_encode($array) and you will get a string value in json notation so you can store in database, and retrive and do a json_decode($string, true) so you cand convert in an array again. If you don't pass the true argument to the json_decode, it will converted to a stdClass.

answered 2009-05-21T18:19:00.530

Your Answer