-2

I have this multidimensional array:

Array
(
    [tennis] => Array
        (
            [3] => Seattle
            [4] => WA
            [5] => 98109
        )

    [Middle East] => Array
        (
            [6] => 2066822513
            [7] => 34740 - Georgetown Keg - 1/2 Manny Pale
            [0] => Hello World
        )

    [Florida] => Array
        (
            [1] => 38380 - Thirteen Coins-Boren
            [2] => 125 Boren Ave N
            [3] => Seattle
        )

)

I am looking for a way to best store it into one single cell in my SQL-DB. The keys of the main array (tennis, middle east, florida) have to be kept. The other keys of the subarrays can be omitted. How can I do that?

2

3 Answers 3

1

I read about the serialize function, and implemented it.

The solution: First I turn the array into a string:

$array = serialize($array);

Then I store the string into the DB cell.

To get back the array, I unserialize the string from the DB cell:

$array = unserialize($array);

And I end up with the same array. Simple

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

Comments

0

Look at the serialize function.

1 Comment

thank you penartur. I've done that and posted a detailed solution
0

I would use JSON since it's more universal.

<?php
  $json=addslashes(json_encode($array));
  mysql_query("INSERT INTO table (data) VALUES ('$json')");
?>

For really large blobs you could do gzdeflate() in that case I recommend also saving a checksum.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.