How extract array from string json? - php

I have this string :
$string = '{"2r0ij":{"id":"2r0ij","title":"Official Reddit announcements","subscribers":7225390}}'
and I want convert to array. I tried with json_decode($string), but result is null. Same result with print_r($string).
What is wrong?

If you want to convert it to array, you need to set the second parameter of json_decode() to true:
$array = json_decode($string, true);
print_r($array);

You do need to use json_decode() with the 'true' flag -
$string = '{"2r0ij":{"id":"2r0ij","title":"Official Reddit announcements","subscribers":7225390}}';
$foo = json_decode($string, true);
print_r($foo);
echo $foo['2r0ij']['title'];
The result is:
Array ( [2r0ij] => Array ( [id] => 2r0ij [title] => Official Reddit announcements [subscribers] => 7225390 ) )
Official Reddit announcements
Here is a working example. The string, minus the opening and closing quotes, validates at JSONLint.

Related

Convert array string into an array Laravel

I've saved some users ids in the database like this:
column user_ids: [2,1]
When I show column's value is "[2,1]"
How can I convert this to an array!
It's a valid Json string. You can use json_decode to get the array.
json_decode("[2,1]");
You can try this code :
<?php
$string = "[2,1]";
$result = json_decode($string);
print_r($result);
?>
Output :
Array
(
[0] => 2
[1] => 1
)

PHP insert keys in string

I have the following string:
{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}
How can I get rid of the numbers preceding "http..." and transform this same numbers in array keys?
Like this:
[0] => "http...",
[1] => "http...",
[2] => "http...",
That looks like a JSON string, so you could decode it.
You could try
$array = json_decode($string, true);
You may also need to reindex the array so it is 0 based; so something like
$array = array_values(json_decode($string, true));
you are having an array data in JSON formation. you have to use a PHP function json_decode to get an result.
$php_array = json_decode($your_array[0], true);
//to see your array
print_r($php_array);
You are missing something I think.
Look at this snippet:
$a=[
0 => '{
"1":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari1.jpg",
"2":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari2.jpg",
"3":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari3.jpg",
"4":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari4.jpg"
}'
];
echo "<pre>";
print_r(json_decode($a[0],TRUE));
it returns:
Array
(
[1] => http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg
[2] => http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg
[3] => http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg
[4] => http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg
)
This will work considering that the array value is a "string" containing a json object.
A var_dump on your array will give you an exact idea on what type the array value is.
Hope this helps:
<?php
//you might want to convert the JSON string to an array:
$json = '{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}';
$array = json_decode($json);
var_dump($array);
// here you already have the json converted to an php array
$arrayWithoutStrangeIndexes = [];
foreach($array as $index => $content){
$arrayWithoutStrangeIndexes[]= $content;
}
// here is just your array with plain data
var_dump($arrayWithoutStrangeIndexes);

IOS Dictionary to php array

I have IOS dictionary like
[{\"my_id_one\":16403,\"my_id_two\":7239}]
When I decode using json_decode, it returns null.
What is best way to decode it to convert it to a PHP array?
Take a look at stripcslashes():
$str = '[{\"my_id_one\":16403,\"my_id_two\":7239}]';
$str_unescaped = stripcslashes($str);
$array = json_decode($str_unescaped, true);
print_r($array);
This outputs:
Array ( [0] => Array ( [my_id_one] => 16403 [my_id_two] => 7239 ) )

Convert a Hashmap string into an array using PHP

I am using an API which returns data as a string in the following format:
{"domain.com":{"status":"available","classkey":"domcno"}}
I want to put this result in to a multi-dimensional PHP array, but since the input is a string, I can't think of a convenient way to convert this.
Is there a function that can automatically parse this data into an array as desired?
That's JSON, simple enough:
$array = json_decode($string, true);
Yields:
Array
(
[domain.com] => Array
(
[status] => available
[classkey] => domcno
)
)
$j = '{"domain.com":{"status":"available","classkey":"domcno"}}';
$d = json_decode($j,true);
print_r($d);

PHP string to array conversion error

I have a php string $data.
on echo $data; I get
{"_index":"movies","_type":"movie","_id":"4","_version":10,"created":false}
I want the string to split in array. So that,
$data["_index"]=movies etc.
I tried $data=array($data);
but got Array ( [0] => {"_index":"movies","_type":"movie","_id":"4","_version":10,"created":false} )
How to correctly obtain array.
That is JSON. Just use json_decode:
$array = json_decode($data, true);
print_r($array);

Categories