如何获得php数组值[复制]

时间:2021-08-23 13:43:53

This question already has an answer here:

这个问题已经有了答案:

I want to insert an array into my db but I just want the values.

我想将一个数组插入到db中,但我只想要值。

Right now it's giving me all of this

现在它给了我所有这些。

a:1:{i:0;s:38:"ARRAY VALUE";}

I just want the part "ARRAY VALUE" to insert into db.

我只是想要将部分“数组值”插入到db中。

Thank you

谢谢你!

4 个解决方案

#1


0  

Hope this helps, I assume you want to save only array values not keys in database

希望这能有所帮助,我假设您只想保存数组值而不是数据库中的键。

    //I assume your array is like this 
    $array = array('a'=>20,'b'=>40,'c'=>60);
    //get array values
    $newArray = array_values($array);
    $arrToSave = serialize($newArray);

and while retrieving from database you need to use 
$array = unserialize('Retrieved field name');

#2


0  

use unserialize method to convert serialize array to php array

使用unserialize方法将序列化数组转换为php数组。

syntax:

语法:

$result=unserialize("your serialized array which is in db");

#3


0  

Some solutions:

一些解决方案:

$arr = unserialize('a:1:{i:0;s:11:"ARRAY VALUE";}');
# method 1 (array with values)
foreach ($arr as $value) {
    echo "Value is $value";
}
# method 2 (array vith only one value)
$value = $arr[0];
echo "Value is $value";

#4


0  

This is a Answer to the problem of storing the data not the answer to the serialization problem that is also present in the question.

这是对存储数据的问题的回答,而不是对问题中出现的序列化问题的答案。

best way is to use json encode like so

最好的方法是使用json编码。

$var = json_encode($array);
(INSERT TO SQL HERE)

-------------------
RETREIVE VALUE
-------------------

$var = json_decode($row->mysqli_assoc);
// NOT the $var will be a object not a array when it gets out...

THIS IS NOT THE MOST RECOMMENDED WAY OF STORING DATA the right way is to make a new table and use a join... this is more of a easy quick fix for when are testing thingsenter code here

这不是最推荐的存储数据的方法,正确的方法是创建一个新表并使用一个连接…在这里测试thingsenter代码时,这是一个简单的快速解决方案。

#1


0  

Hope this helps, I assume you want to save only array values not keys in database

希望这能有所帮助,我假设您只想保存数组值而不是数据库中的键。

    //I assume your array is like this 
    $array = array('a'=>20,'b'=>40,'c'=>60);
    //get array values
    $newArray = array_values($array);
    $arrToSave = serialize($newArray);

and while retrieving from database you need to use 
$array = unserialize('Retrieved field name');

#2


0  

use unserialize method to convert serialize array to php array

使用unserialize方法将序列化数组转换为php数组。

syntax:

语法:

$result=unserialize("your serialized array which is in db");

#3


0  

Some solutions:

一些解决方案:

$arr = unserialize('a:1:{i:0;s:11:"ARRAY VALUE";}');
# method 1 (array with values)
foreach ($arr as $value) {
    echo "Value is $value";
}
# method 2 (array vith only one value)
$value = $arr[0];
echo "Value is $value";

#4


0  

This is a Answer to the problem of storing the data not the answer to the serialization problem that is also present in the question.

这是对存储数据的问题的回答,而不是对问题中出现的序列化问题的答案。

best way is to use json encode like so

最好的方法是使用json编码。

$var = json_encode($array);
(INSERT TO SQL HERE)

-------------------
RETREIVE VALUE
-------------------

$var = json_decode($row->mysqli_assoc);
// NOT the $var will be a object not a array when it gets out...

THIS IS NOT THE MOST RECOMMENDED WAY OF STORING DATA the right way is to make a new table and use a join... this is more of a easy quick fix for when are testing thingsenter code here

这不是最推荐的存储数据的方法,正确的方法是创建一个新表并使用一个连接…在这里测试thingsenter代码时,这是一个简单的快速解决方案。