what's the different between
Array
(
[user_id] => 1
[username] => user
[first_name] => hello
[last_name] => world
)
and
{
user_id: "1",
username:"user",
first_name:"hello",
last_name:"world"
}
? and how to convert them in php?
Edit: add details
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
)
...
)
So is there any easy way to convert it instead of using for loop to json_decode each element? Thx
The first string is the output of the print_r
function. Example:
$array=array('key'=>'value');
print_r($array);
Outputs:
Array
(
[key] => value
)
Your second is JSON-like, alto it lacks of double-quotes for keys.
You can't convert them directly.
You could convert JSON to a PHP array with json_decode
if your second string was something like this:
{
"user_id": "1",
"username":"user"
...
}
The first one, as stated is PHP and second one is a JavaScript array.
To convert array to JSON, use json_encode()
;
To convert JavaScript array to JSON text and later to PHP array(associative); use JSON.stringify()
and json_decode()
.
For your task, the best bet would be array_walk
function:
array_walk( "json_decode", $MyArry );