php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51384 Allow array keys "0", "1", etc.
Submitted: 2010-03-24 23:34 UTC Modified: 2010-04-30 00:52 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: info at fedushin dot ru Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.2 OS:
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: info at fedushin dot ru
New email:
PHP Version: OS:

 

 [2010-03-24 23:34 UTC] info at fedushin dot ru
Description:
------------
PHP Manual says about arrays: "A key may be either an integer or a string .".
But which strings can be used as key?
Even rather experienced programmer will answer: any, and will be wrong.
In fact, there's no way to use string keys "0", "1", etc. because they are implicitly converted into integer keys.
So, PHP engine's behaviour in this case should be considered as unpredictable.
This problem is rejected in Bug #9307, sorry for repetition.

Then, let's ask our experienced programmer what output will be generated by the following script:
<?php
// Assume we have already made successful mysql_connect().
$res = mysql_query('SELECT 1, "aaa";');
$row = mysql_fetch_array($res);
echo $row[0] . '<br>';
echo $row[1] . '<br>';
?>
I think very few people will give correct answer:
1
1
So again we have unpredictable behaviour.

Suggestion is to improve this situation:
1. Allow storing values in PHP arrays under string keys .., "-1", "0", "1", ...
2. Always use strings & never use integers as associative indices in arrays returned by mysql_fetch_array().

Test script:
---------------
$a       = array();
$a[1]    = 'aaa';
$a['1']  = 'bbb';
var_dump($a);

$res 	= mysql_query('SELECT 1, "aaa";');
$row 	= mysql_fetch_array($res);
var_dump($row);

Expected result:
----------------
array
  1 => string 'bbb' (length=3)
  '1' => string 'bbb' (length=3)

array
  0 => string '1' (length=1)
  '1' => string '1' (length=1)
  1 => string 'aaa' (length=3)
  'aaa' => string 'aaa' (length=3)

Actual result:
--------------
array
  1 => string 'bbb' (length=3)

array
  0 => string '1' (length=1)
  1 => string '1' (length=1)
  2 => string 'aaa' (length=3)
  'aaa' => string 'aaa' (length=3)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-30 00:52 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2010-04-30 00:52 UTC] felipe@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

See bug #45959
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 19:01:30 2024 UTC