在MYSQL中读写PHP数组

时间:2021-01-20 21:35:35

I want to save a PHP associative array from a PHP varible to a MYSQL database, then later access the stored value (within the MYSQL database) and use it within PHP as an associative array.

我想将PHP关联数组从PHP varible保存到MYSQL数据库,然后访问存储的值(在MYSQL数据库中)并在PHP中将其用作关联数组。

$arr = array("abc"=>"ss","aaa"=>"ddd");

now i want to save

现在我想保存

array("abc"=>"ss","aaa"=>"ddd");

to the database and again want to retrive it and assign it to variable.

到数据库并再次想要检索它并将其分配给变量。

I tried to use the serialize function, but it only saved the word "Array" into database.

我尝试使用serialize函数,但它只将“Array”一词保存到数据库中。

2 个解决方案

#1


11  

One way to do this is to serialize it into a string before insert, and then deserialize it into array after fetching. There are different ways to do that, but if your arrays are simple, JSON is an acceptable serialization format.

一种方法是在插入之前将其序列化为字符串,然后在获取后将其反序列化为数组。有不同的方法可以做到这一点,但如果您的数组很简单,JSON是一种可接受的序列化格式。

You could json_encode on the way in:

您可以在途中使用json_encode:

$str = json_encode($arr);
// Insert $str into db

Then json_decode later:

然后json_decode:

// Got $str from db
$arr = json_decode($str);

Another method is serialize:

另一种方法是序列化:

$str = serialize($arr);
// Insert $str into db

And unserialize:

// Got $str from db
$arr = unserialize($str);

This will allow more possibilities for what you can serialize than json_encode and json_decode, but it will be harder to inspect the database manually to see what's in there.

这将为您提供比json_encode和json_decode更多的序列化可能性,但是手动检查数据库以查看其中的内容将更加困难。

So both methods have advantages and disadvantages. There are other serialization/marshal formats out there too.

所以这两种方法都有优点和缺点。还有其他序列化/编组格式。

#2


1  

As Ben said, you need to serialize your array before storing it in the database then unserialize it when you read it back. If 'Array' is being written to your database then you are probably not saving the results of serialize() to the variable that you are writing.

正如Ben所说,您需要先将数组序列化,然后再将其存储在数据库中,然后在读取数据时对其进行反序列化。如果正在将“数组”写入数据库,那么您可能不会将serialize()的结果保存到您正在编写的变量中。

<?php

function store()
{
    $arr = array("abc"=>"ss","aaa"=>"ddd");

    $serialized = serialize($arr);

    // Store $serialized to the database
}

function retrieve()
{
    // Retrieve $serialized from the database

    $arr = unserialize($serialized);
}

#1


11  

One way to do this is to serialize it into a string before insert, and then deserialize it into array after fetching. There are different ways to do that, but if your arrays are simple, JSON is an acceptable serialization format.

一种方法是在插入之前将其序列化为字符串,然后在获取后将其反序列化为数组。有不同的方法可以做到这一点,但如果您的数组很简单,JSON是一种可接受的序列化格式。

You could json_encode on the way in:

您可以在途中使用json_encode:

$str = json_encode($arr);
// Insert $str into db

Then json_decode later:

然后json_decode:

// Got $str from db
$arr = json_decode($str);

Another method is serialize:

另一种方法是序列化:

$str = serialize($arr);
// Insert $str into db

And unserialize:

// Got $str from db
$arr = unserialize($str);

This will allow more possibilities for what you can serialize than json_encode and json_decode, but it will be harder to inspect the database manually to see what's in there.

这将为您提供比json_encode和json_decode更多的序列化可能性,但是手动检查数据库以查看其中的内容将更加困难。

So both methods have advantages and disadvantages. There are other serialization/marshal formats out there too.

所以这两种方法都有优点和缺点。还有其他序列化/编组格式。

#2


1  

As Ben said, you need to serialize your array before storing it in the database then unserialize it when you read it back. If 'Array' is being written to your database then you are probably not saving the results of serialize() to the variable that you are writing.

正如Ben所说,您需要先将数组序列化,然后再将其存储在数据库中,然后在读取数据时对其进行反序列化。如果正在将“数组”写入数据库,那么您可能不会将serialize()的结果保存到您正在编写的变量中。

<?php

function store()
{
    $arr = array("abc"=>"ss","aaa"=>"ddd");

    $serialized = serialize($arr);

    // Store $serialized to the database
}

function retrieve()
{
    // Retrieve $serialized from the database

    $arr = unserialize($serialized);
}