php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27712 unserialize / Array access
Submitted: 2004-03-26 05:38 UTC Modified: 2004-04-08 13:48 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: hannes at phpug dot ch Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2004-04-07 OS: *
Private report: No CVE-ID: None
 [2004-03-26 05:38 UTC] hannes at phpug dot ch
Description:
------------
Unserialize works perfectly and produces an array with like 
array("0" => "foo"). The same serialized string gets unserialized differently in PHP4, there it's array(0 => "foo"). 
I cannot access the content of the PHP5 result, because it seems that my lookup for "1"(string) gets converted to one for 1 (int). Which fails.

Tested with:

-PHP 4.3.5RC2-dev (cli) (built: Jan 15 2004 16:58:31)
-PHP 5.0.0RC2-dev (cli) (built: Mar 21 2004 17:58:48)

I'm not sure if this is really something that needs to be fixed, unserialize works correctly, but not as expected.


Reproduce code:
---------------
<?php
error_reporting(E_ALL);

$serializedNames = 'a:2:{s:1:"0";s:6:"hubert";s:1:"1";s:5:"waldo";}';
$names =  unserialize($serializedNames);

var_export($names); //works, but...
/*
php5:
array (
  '0' => 'hubert',

php4:
array (
  0 => 'hubert',
  ..
print($names["1"]); // php5: doesn't work. php4: works;
print($names[1]);   // php5: doesn't work  php4: works;
?>

Expected result:
----------------
array (
  0 => 'hubert',
  1 => 'waldo',
)waldowaldo

Actual result:
--------------
array (
  '0' => 'hubert',
  '1' => 'waldo',
)
PHP Notice:  Undefined index:  1 in ....


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-07 07:49 UTC] sniper@php.net
<?php

error_reporting(E_ALL);

$nam = array ("0" => "hubert", "1" => "waldo");
$ser = serialize ($nam);
echo $ser, "\n";

$unser = unserialize ($ser);

var_export($unser);
?>

Using that script, I can NOT get such serialized string as you did. Note: There was some bug in this earlier but nowadays serialize() works fine..
 [2004-04-08 13:48 UTC] hannes at phpug dot ch
Yous see, the example string above does not come from PHP. Other tools that interact with PHP have adopted the format. The string above can be unserialized in PHP4 and in PHP5, the result can not be accessed using the indices.
If you feel this is not PHP's fault, just close this bug. :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 21:01:33 2024 UTC