how to parse the following string to json in php - 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
)

Related

Could not get data from serialized array using unserialize function

I have serialized data and I am using PHP unserialize function which is not working for me. I do not know what is wrong with this string. This is how my serialized data look like. My PHP knowledge is limited so I could not figure out the issue in this data. Can any one help me with this.
s:73:"a:5:{i:0;s:4:"8941";i:1;s:4:"8939";i:2;s:4:"8942";i:3;s:4:"8946";i:4;s:4:"8950";}";
You have a serialized string that contains a serialized array. The string length is 81 not 73.
s:81:"characters in between the first and last quotes and in the example there are 81"
$string = 's:81:"a:5:{i:0;s:4:"8941";i:1;s:4:"8939";i:2;s:4:"8942";i:3;s:4:"8946";i:4;s:4:"8950";}";';
$result = unserialize($string);
Yields the serialized array:
a:5:{i:0;s:4:"8941";i:1;s:4:"8939";i:2;s:4:"8942";i:3;s:4:"8946";i:4;s:4:"8950";}
Unserialize that:
$array = unserialize($result);
Yields the array:
Array
(
[0] => 8941
[1] => 8939
[2] => 8942
[3] => 8946
[4] => 8950
)

PHP Extract only a specific portion in a string between two characters

How can i extract only the substring ..\/files\/listo_bfty77a3.jpg from the main string below?
[
{
"name":"..\/files\/listo_bfty77a3.jpg",
"usrName":"Listo.jpg",
"size":188126,
"type":"image\/jpeg",
"searchStr":"Listo.jpg,!:sStrEnd"
}
]
Practically i would need the content starting from the eleventh char and end at the first ".
Thanks in advance
The string you posted is the JSON representation of some data structure. Decode it using the PHP function json_decode(); pass TRUE as its second argument to get arrays back:
$text = '[
{
"name":"..\/files\/listo_bfty77a3.jpg",
"usrName":"Listo.jpg",
"size":188126,
"type":"image\/jpeg",
"searchStr":"Listo.jpg,!:sStrEnd"
}
]';
$data = json_decode($text, TRUE);
If you don't speak JSON or the data structure is thick and the JSON cannot be understood at the first glance, use print_r() to see how the data structure looks like.
print_r($data);
It displays:
Array
(
[0] => Array
(
[name] => ../files/listo_bfty77a3.jpg
[usrName] => Listo.jpg
[size] => 188126
[type] => image/jpeg
[searchStr] => Listo.jpg,!:sStrEnd
)
)
It's clear now that the property you need can be accessed as $data[0]['img'].

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

Store an array to an array key

I'm trying to store an array ($temp) into the $data array, where key is prices.
$data['prices'] = $temp;
However, PHP converts the array into string instead and is throwing me and error.
Notice: Array to string conversion
Is $data = array('prices' => $temp); the only solution?
edit:
I found my mistake. Yes, $data was used previously as a string that's why PHP is converting the input into string.
Problem 2, doing a print_r on $data['prices'] = $xml->result->Prices->Price, shows only 1 set of array. But I am able to retrive 2 sets of result by doing a foreach loop on $data['prices']. Why is that so?
Content of $temp http://pastebin.com/ZrmnKUPB
Let me be more clear..
The full xml object I'm trying to extract information from: http://pastebin.com/AuMJiyrw
I'm only interested in the price array (Price_strCode and Price_strDescription) and store them in $data['prices']. End result something like this:
Array(
[0] => (
[Price_strCode] => 0001
[Price_strDescription] => Gold
)
[1] => (
[Price_strCode] => 0002
[Price_strDescription] => Silver
)
)
Unless you are doing some other array to string conversion elsewhere, the array is actually being stored as another array.
$data['prices'] will be an array, which can be accessed as $data['prices']['key'].
This is not possible, I have been doing this always and it works fine. You must be doing something else somewhere which is causing your array to convert to strong. share your code here

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