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 ) )
Related
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);
I have the following string that I receive from an API call:
a = "{
"option1"=>"Color",
"attribute1"=>{0=>"Black", 1=>"White",2=>"Blue"},
"option2"=>"Size",
"attribute2"=>{0=>"S", 1=>"L",2=>"M"}
}"
I would like to convert it to a JSON array; So, I have tried JSON_encode(), but it returns the following string:
""{\"option1\"=>\"Color\",\"attribute1\"=>{0=>\"Black\", 1=>\"White\",2=>\"Blue\"},\"option2\"=>\"Size\",\"attribute2\"=>{0=>\"S\", 1=>\"L\",2=>\"M\"}}""
Could you please advise me on how to achieve what i want.
Thanks
The preferable way would be affecting the service which gives you such kind of strings to get a valid JSON string(if it's possible). At the moment, if it's about adapting some "arbitrary" string to JSON notation format and further getting a JSON "array" use the following approach with preg_replace and json_decode functions:
$json_str = '{
"option1"=>"Color",
"attribute1"=>{0=>"Black", 1=>"White",2=>"Blue"},
"option2"=>"Size",
"attribute2"=>{0=>"S", 1=>"L",2=>"M"}
}';
// To get a 'pure' array
$arr = json_decode(preg_replace(["/\"?(\w+)\"?=>/", "/[\r\n]|\s{2,}/"], ['"$1":', ''], $json_str), true);
print_r($arr);
The output:
Array
(
[option1] => Color
[attribute1] => Array
(
[0] => Black
[1] => White
[2] => Blue
)
[option2] => Size
[attribute2] => Array
(
[0] => S
[1] => L
[2] => M
)
)
To get a JSON string representing an array:
$json_arr = json_encode($arr);
print_r($json_arr);
The output:
{"option1":"Color","attribute1":["Black","White","Blue"],"option2":"Size","attribute2":["S","L","M"]}
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);
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);
I had php multi dimensional array
Array
(
[0] => Array
(
[WorkHrs] => 9826
[Focus_Date] => 2010-02-10
)
[1] => Array
(
[WorkHrs] => 9680
[Focus_Date] => 2010-02-11
)
)
and I want to convert it in Javascript to
myArray = [['2010-02-10', 9826],['2010-02-11', 9680]];
$jsArray = array();
foreach($myArray as $array) {
$jsArray[] = array($array['Focus_Date'], (int) $array['WorkHrs']);
}
echo json_encode($jsArray);
echo json_encode(array_map(array_values, $arr));
EDIT: To get it in the specified order:
function to_focus_work_array($arr)
{
return array($arr['Focus_Date'], $arr['WorkHrs']);
}
echo json_encode(array_map('to_focus_work_array', $arr));
json_encode
That's pretty much exactly what json_encode does. Input is a PHP-array (other datatypes accepted), output is what you describe.
have you tried the json_encode()?
refer to http://php.net/manual/en/function.json-encode.php