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.

1
  • 2
    Just a small comment, you can simplify your unserialization code from those 3 lines to just one: $array2 = unserialize($row['Array']); Commented May 21, 2009 at 15:15

4 Answers 4

10

Your code looks ok...

One thing that can catch you out is if your column is too small - if you use VARCHAR(255) your data can be truncated and won't unserialize. If you add the value of $row['Array'] I could see if it's whole.

Sign up to request clarification or add additional context in comments.

Comments

4

Use column type TEXT. Serialized data often doesn't fit VARCHAR(255).

Comments

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.

Comments

0
Particulars Quantity Rate Amount Amount Sum       Others      Others Amount                        Tax            Total                         

This is my table in php/html. I need to store and retrieve the php Particulars(p),Quantity(q),Rate(r) as a separate table format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.