From:             
Operating system: 
PHP version:      5.3.2
Package:          Arrays related
Bug Type:         Bug
Bug description:Allow array keys "0", "1", etc.

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)

-- 
Edit bug report at http://bugs.php.net/bug.php?id=51384&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=51384&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=51384&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=51384&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=51384&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=51384&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=51384&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=51384&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=51384&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=51384&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=51384&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=51384&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=51384&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=51384&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=51384&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=51384&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=51384&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=51384&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=51384&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=51384&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=51384&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=51384&r=mysqlcfg

Reply via email to