Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array after unserialize cannot get value by key

Tags:

php

Array cannot get value by key from unserialize. It show error Undefined offset, but the array has the index call "1134". How can I get the index 1134 value?

$original = unserialize('O:8:"stdClass":1:{s:4:"1134";i:1;}');

$result = (array)$original;
print_r ($result); //Array ( [1134] => 1 ) 

print_r($result["1134"]); //Undefined offset: 1134
print_r($result['1134']); //Undefined offset: 1134
print_r($result[1134]); //Undefined offset: 1134
like image 612
Tomislav Fridwald Avatar asked Jul 21 '26 08:07

Tomislav Fridwald


1 Answers

You've to iterate over your unserialized data and then store it into an array:

<?php
$original = unserialize('O:8:"stdClass":1:{s:4:"1134";i:1;}');
$arr = [];
foreach($original as $key => $values) {
    $arr[$key] = $values;
}
echo $arr[1134] // outputs 1
?>

Output:-https://3v4l.org/B94OS#v5638

like image 161
AnTrakS Avatar answered Jul 24 '26 01:07

AnTrakS