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
)
Related
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
)
I am using jQuery to convert form data to serialized form using:
var data = $('#frm').serialize();
In php I get this:
fiscalyear_id=4&category=Category+A&isgraph=on&Title=a&Value=a&Title=b&Value=b&category=Category+B&Title=c&Value=c&Title=d&Value=d&category=Category+C&Title=e&Value=e&Title=f&Value=f&data;=&csrf_check=9c288285b379701b27c3836091c00b04
And when I do:
parse_str($_POST['data'], $data);
pretty_print($data);
I get:
Array
(
[fiscalyear_id] => 4
[category] => Category C
[isgraph] => on
[Title] => f
[Value] => f
[data;] =>
[csrf_check] => 9c288285b379701b27c3836091c00b04
)
As can be seen, not all paramters are coming in array above. Does anyone have an idea what I am doing wrong ? Thanks for the help
parse_str parses the string in variable and you are getting it into array.
but duplicate array keys are not possible,
hence you are not getting all the values because they have the same key!
I have seen many posts on here about converting a multi dimensional array into a string but not the other way around so I have a question to ask. I have got the following string of data which is retrieved from a JQuery array via a post:
["enquiry#gardengamesltd.co.uk, sales#gardengamesltd.co.uk","http://www.gardengamesltd.co.uk/acatalog/contactus.html"],["enquiry#gardengames.com","http://www.gardengames.com/contact/"],["info#gardengamesandleisure.com","http://www.gardengamesandleisure.com/ContactUs.aspx"],["playtime#kentgardengameshire.com","http://www.kentgardengameshire.com/contact-us.html"],["sales#gardengamesuk.com","http://www.gardengamesuk.com/contact.php"],["team#gardenknightgames.com","http://www.gardenknightgames.com/contact/"],["ajax-loader#2x.gif","http://www.just-garden-games.co.uk/"]
What I am wanting to do is convert it into an array which looks like so:
Array
(
[0] => Array
(
[Email] => enquiry#gardengamesltd.co.uk, sales#gardengamesltd.co.uk
[FB] => http://www.gardengamesltd.co.uk/acatalog/contactus.html
)
[1] => Array
(
[Email] => enquiry#gardengames.com
[FB] => http://www.gardengames.com/contact/
)
[2] => Array
(
[Email] => info#aaeventhire.com
[FB] => http://www.aaeventhire.com/pricing/garden-games
)
)
I realize I could use $array = explode('","', $harvest_data); however this is only going to give me a single level array and ideally I am wanting to keep email, fb inside an inner array.
Has anyone got any ideas on how I can go about doing this?
Thanks.
As it is, your string is not valid JSON. Wrapping it in a pair of []'s would work in this case so if the input always has this form, this would work:
$json_string = '[' . $your_string . ']';
$your_array = json_decode($json_string);
However, it would be best to make sure that your front-end / javascript posts valid JSON to begin with.
Working example.
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
I have an array which prints like this
Array ( [0] => 1691864 [1] => 7944458 [2] => 9274078 [3] => 1062072 [4] => 8625335 [5] => 8255371 [6] => 5476104 [7] => 6145446 [8] => 7525604 [9] => 5947143 )
If I json_encode($thearray) I get something like this
[1691864,7944458,9274078,1062072,8625335,8255371,5476104,6145446,7525604,5947143]
Why the name is not encoded (e.g 0, 1 , 2 , 3 etc) ? and how should I do to make it appear in the json code?
the full code is below
$ie = 0;
while($ie 10)
{
$genid = rand(1000000,9999999);
$temp[$ie] = $genid ;
$ie++;
}
print_r($temp);
$temp_json = json_encode($temp);
print_r($temp_json);
You can force that json_encode uses an object although you’re passing an array with numeric keys by setting the JSON_FORCE_OBJECT option:
json_encode($thearray, JSON_FORCE_OBJECT)
Then the returned value will be a JSON object with numeric keys:
{"0":1691864,"1":7944458,"2":9274078,"3":1062072,"4":8625335,"5":8255371,"6":5476104,"7":6145446,"8":7525604,"9":5947143}
But you should only do this if an object is really required.
Use this instead:
json_encode((object)$temp)
This converts the array into object, which when JSON-encoded, will display the keys.
If you are storing a sequence of data, not a mapping from number to another number, you really should use array.
Because those are just the indices of the array. If you want to add some kind of name to each element then you need to use an associative array.
When you decode that JSON array though it will come back out to 0, 1, 2, 3 etc.
This is defined behaviour. The array you show is a non-associative, normally indexed array. Its indexes are implicitly numeric.
If you decode the array in PHP or JavaScript, you will be able to access the elements using the index:
$temp_array = json_decode($temp_json);
echo $temp_array[2]; // 9274078