php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47211 Array handling problem
Submitted: 2009-01-24 20:28 UTC Modified: 2009-01-24 23:07 UTC
From: usmanghanee at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.0alpha3 OS: Windows XP (Service Pack 2)
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: usmanghanee at gmail dot com
New email:
PHP Version: OS:

 

 [2009-01-24 20:28 UTC] usmanghanee at gmail dot com
Description:
------------
I don't know exactly how to explain it. Forgive me if I use wrong English. I'll try to be as short as possible.

I tried to use two dimensional array and faced a strange problem.

Problem Brief:

First I defined two dimensional array. 
Then, I tried to access a third dimension of such array which was not actually defined at all. The result should have been a warning saying something like "Undefined index" but it printed out some value. The value printed was the first letter of the value at 2nd dimension based offset. Initially, I tried different(variety of) alphabetic and alphanumeric values at third level dimension. It kept on printing the first letter of the value as mentioned above. Then, I tried purely numeric values like '0', '1', '2' and so on. This time it behaved differently. It printed out the letter at offset number, provided as third dimension, from the value at 2nd dimension.

I tested it with 5.2.8 and 5.3.0Alpha both. The problem also exists in version 5.3.0Alpha. I also tried the command line methods for both versions which resulted out in the same way.

Reproduce code:
---------------
$arr = array("sub1" => 
                   array("sub2" => "SomeString"));

/*
    // I also tried it as the code given in these comments.
    // It gave the same output.
    $arr["sub1"] = array();
    $arr["sub1"]["sub2"] = "SomeString";
*/

echo $arr["sub1"]["sub2"]["sub3"];

Expected result:
----------------
There should have been some warning saying "Undefined index - sub3".

Actual result:
--------------
The actual result was an "S" printed on the screen, the first letter of the value "SomeString".


/*
   Please Note:
   ------------
   1) When I provided '0' instead of "sub3" as the third dimension for echo purpose, it printed out "S" which is the letter at offset '0' of the value "SomeString".
   2) When I provided '1' instead of "sub3" as the third dimension for echo purpose, it printed out "o" which is the letter at offset '1' of the value "SomeString".
   3) When I provided '2' instead of "sub3" as the third dimension for echo purpose, it printed out "m" which is the letter at offset '2' of the value "SomeString".
   4) So on.
*/

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-24 23:07 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$foo = \"some String\";
$foo[0] == \"s\";

and in oyur case the key is casted to int (0)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Dec 02 14:00:01 2025 UTC