Convert a Hashmap string into an array using PHP - 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);

Related

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);

how to parse the following string to json in php

I need small as key and url as value for example :-
small->upload\2016\04\greenfield-100x100.jpg
"a:3:{s:5:\"small\";s:39:\"\/uploads\/2016\/04\/greenfield-100x100.jpg\";s:6:\"medium\";s:39:\"\/uploads\/2016\/04\/greenfield-300x200.jpg\";s:5:\"large\";s:39:\"\/uploads\/2016\/04\/greenfield-500x400.jpg\";}""
You need to use unserialize for getting the data as Readable / Understandable. Your data is valid.
unserialize() takes a single serialized variable and converts it back
into a PHP value.
$data = "a:3:{s:5:\"small\";s:39:\"/uploads/2016/04/greenfield-100x100.jpg\";s:6:\"medium\";s:39:\"/uploads/2016/04/greenfield-300x200.jpg\";s:5:\"large\";s:39:\"/uploads/2016/04/greenfield-500x400.jpg\";}";
$out = unserialize($data);
print_r($out);
The Result after unserialize.
Array
(
[small] => /uploads/2016/04/greenfield-100x100.jpg
[medium] => /uploads/2016/04/greenfield-300x200.jpg
[large] => /uploads/2016/04/greenfield-500x400.jpg
)

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 ) )

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);

Type Casting an Decoded Json Object to array, generate error while accessing it

I decoded a json string and then Type casted it into any Array and tried to access it later. But it generate Undefined Index Error
Here is my sample code
$json = '{"1":"Active","0":"Inactive"}'; //Yes, it is a valid Json String
$decodedObject = json_decode($json);
$array = (array)$decodedObject;
echo $array['1']; // This generates undefinded Index 1 Error
Here is the display of the array and object
stdClass Object
(
[1] => Active
[0] => Inactive
)
Array
(
[1] => Active
[0] => Inactive
)
1.) instead of making it in two steps how about doing it like:
$decodedArray = json_decode($json, true);
it will directly give you the array instead of object
2.) make sure your json code is corret:
{"1":"Active","0":"Inactive"}
3.) your var_dump shows that array{[1]=>.... so why refering it like $array['1'] can it be even simpler $array[1]
No, it is not a valid JSON string (JSONLint is your friend). You used a comma instead of a colon:
{"1":"Active","0","Inactive"} // invalid
{"1":"Active","0":"Inactive"} // valid

Categories