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
)
I've looked at a number of suggestions for this, and they seem to rely on array_combine() which unfortunately is not suitable, as they arrays need to stay separate for other functions.
The arrays are set out as per
Array ( [0] => 3 [1] => 1 [2] => 3 )
Array ( [0] => 194 [1] => 0 [2] => 452 )
When I read in the first array, I get the key as $key and everything works fine.
Whenever I try and access the second array the whole script just whites out the page.
I want the code to work simliar to this ...
$a2value = $a2[$key] => $value;
echo $a2value;
Then I can do SQL lookups using $a2value
Any help would be appreciated
Here Try this
let's suppose two arrays of same number of elements
$a1=[1,2,3];
$a2=[194,0,452];
//A Simple foreach
foreach($a1 as $key=>$a1value){
$a2value=$a2[$key];
//Perform Query here
}
Remember one thing number of elements of both array should always be equal or it will cause an error
If you aren't certain about the size of both arrays then replace the line with this code
$a2value=empty($a2[$key]) ? null : $a2[$key];
Hope this works !!!
I am using an API which has a lot of data inside lots of arrays which as you may know can be quite confusing.I am relatively new to API's and this one in particular has no documentation.
My code below is grabbing the recent_games() function which is pulling the whole API then I am using foreach loops to get inside the data.
$games = $player->recent_games();
foreach($games['gameStatistics']['array'] as $key => $gameStatistic) {
$game_date[strtotime($gameStatistic['createDate'])] = $gameStatistic;
}
// order data
krsort($game_date);
foreach ($game_date as $game => $data) {
$statistics[$data] = $data['statistics'];
}
I am getting errors such as illegal offset for:
$statistics[$data] = $data['statistics'];
Is there a way to continue down the nesting of arrays ($game_date) to get to the data that I need?
Let me know if you need more info.
Thanks
EDIT more info:
The first foreach loop at the top loops a unix timestamp key per game. Looks like this:
[1370947566] => Array
(
[skinName] => Skin_name
[ranked] => 1
[statistics] => Array
(
[array] => Array
(
[0] => Array
(
[statType] => stat_data
[value] => 1234
)
[1] => Array
(
[statType] => stat_data
[value] => 1234
)
As you can see its quite nested but I am trying to get to the individual statistics array. I hope that helps?
$statistics[$data] = $data['statistics'];
There is absolutely no way this line is correct.
The right hand side uses $data as if it were an array, indexing into it. The left hand side uses $data as a key into an array. Since the only valid types for keys are strings and integers, $data cannot satisfy the requirements of both expressions at the same time -- it cannot be an array and a string or integer.
It's obvious from the error message that $data is in fact an array, so using it as $staticstics[$data] is wrong. What do you want $statistics to be?
I have been working with parsing some remote JSON with PHP. I have been able to download the JSON and assign it to a variable, and I have used the array functionality with json_decode:
$data = json_decode($remotejson, true);
I have then printed the complete array back to verify the contents of the array:
echo print_r($data);
The array prints back and I can see the keys and values:
[files] => Array
(
[/photogalleryupload.thumbs/1934307_000001.jpg] => Array
(
[source] => derivative
[format] => Thumbnail
[original] => moviefile_1934307.mp4
)
I am trying to get the value of the first nested key name which is "/photogalleryupload.thumbs/1934307_000001.jpg" and assign it to a variable.
For example, I would like the following code:
echo $data['files'][0];
To return this:
/photogalleryupload.thumbs/1934307_000001.jpg
This does not work.
The difficulty I am having is that this value I am trying to return is a 2nd level key name and I have been having trouble finding a way of assigning it to a variable.
$keys = array_keys($data['files'])
$key = $keys[0]
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