php:array()和{}之间的区别 [英] php: different between array( ) and { }
问题描述
Array
(
[user_id] => 1
[username] => user
[first_name] => hello
[last_name] => world
)
和
{
user_id: "1",
username:"user",
first_name:"hello",
last_name:"world"
}
?以及如何在php中转换它们?
? and how to convert them in php?
添加详细信息
实际上,我正在使用redis zunionstore和zrevrange来组合用户数据.结果返回给我类似的东西:
Actually i'm using redis zunionstore and zrevrange to combine user data. The result returns me something like :
Array
(
[0] =>{"user_id":"1","username":"user","first_name:hello","last_name:world"}
[1] => ...
)
因为我必须处理数据,所以我想要这样的东西:
Since i have to process the data, i want something like this:
Array
(
[0] => Array
(
[user_id] => 1
[username] => user
[first_name] => hello
[last_name] => world
)
...
)
那么有什么简单的方法可以转换它,而不是使用for循环对每个元素进行json_decode?谢谢
So is there any easy way to convert it instead of using for loop to json_decode each element? Thx
推荐答案
第一个字符串是 print_r
函数的输出.示例:
The first string is the output of the print_r
function. Example:
$array=array('key'=>'value');
print_r($array);
输出:
Array
(
[key] => value
)
您的第二个是类似于JSON的,而且还缺少密钥的双引号.
您不能直接将其转换.
Your second is JSON-like, alto it lacks of double-quotes for keys.
You can't convert them directly.
如果第二个字符串是这样的,则可以使用 json_decode
将JSON转换为PHP数组:
You could convert JSON to a PHP array with json_decode
if your second string was something like this:
{
"user_id": "1",
"username":"user"
...
}
这篇关于php:array()和{}之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!