|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-05-14 20:09 UTC] josefrancklin at yahoo dot com dot br
Description:
------------
Code typed below generates unexpected result in the [0] term from an array.
Test script:
---------------
for ($i=0;$i<3;$i++)
{
$array_id=array( 0 => 0 , 1 => 1 , 2 => 2 );
$array_id[$i]="x";
for ($j=0;$j<3;$j++)
{
print "term [$j] is $array_id[$j]";
if ($array_id[$j]!=$j)
print ", not $j";
print "<BR>\n";
}
print "<BR>\n";
}
Expected result:
----------------
term [0] is x, not 0
term [1] is 1
term [2] is 2
term [0] is 0
term [1] is x, not 1
term [2] is 2
term [0] is 0
term [1] is 1
term [2] is x, not 2
Actual result:
--------------
array[0] is x
array[1] is 1
array[2] is 2
array[0] is 0
array[1] is x, not 1
array[2] is 2
array[0] is 0
array[1] is 1
array[2] is x, not 2
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 17:00:01 2025 UTC |
//for ($i=0 ; $i<3 ; $i++){ // first loop: $i is 0; $array_id = array( 0 => 0 , 1 => 1 , 2 => 2 ); $array_id[$i] = "x"; // $i is 0, then $array_id[0] = "x" // print_r($array_id) here results Array ( [0] => x [1] => 1 [2] => 2 ) for ($j=0 ; $j<3 ; $j++) { if ($array_id[$j] != $j) // $j is 0, then if ($array_id[0] != 0) print ", not $j"; // $array_id[0] is "x" (!=0), but nothing printed //((problem, how to solve???)) } // second loop: $i is 1; $array_id = array( 0 => 0 , 1 => 1 , 2 => 2 ); $array_id[$i] = "x"; // $i is 1, then $array_id[1] = "x" // print_r($array_id) here results Array ( [0] => 0 [1] => x [2] => 2 ) for ($j=0; $j<3; $j++) { // lets go to $j=1... if ($array_id[$j] != $j) // $j is 1, then if ($array_id[1] != 1) print ", not $j"; // $array_id[1] is "x" (!=1), is's printed ((ok)) } //} Problem... I've tried several forms to change the code in order to work correct but no success... I think I don't know programming... (and write in english too) -.-In your original code, change: if ($array_id[$j]!=$j) to be: if ($array_id[$j]!==$j)