Monday, 15 February 2010

php - Define array with variable index -



php - Define array with variable index -

my code:

$rank_content = file('https://www.championsofregnum.com/index.php?l=1&ref=gmg&sec=42&world=2'); $line_count = 0; //initializing first few keys because of no reason (the latter ones aren't in utilize yet) $ranknamearr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72); $rankrlmparr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72); while ($line = array_shift($rank_content)) { // retrieving line after line of website, work indeed $line_count += 1; if(strpos($line, "warrior #") || strpos($line, "archer #") || strpos($line, "mage #")) { $ranknamearr[$line_count] = $line_count + 1; // here nil happens $rankrlmparr[$line_count] = $line_count + 2; // nil happens here, } }

why does

echo $ranknamearr[2]; echo $rankrlmparr[2];

give me value 42 instead of right value? if replace $line_count real number, script works properly.

my intention store value $line_count + 1 $ranknamearr @ position $line_count. not complicated actually

edit ------------- forget above, please. reduced script actual problem:

$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42); $counter=0; for($i=0;$i<7;$i++) { $arr[$counter]=$i; } echo $arr[5];

this sadly echoes 42. have no thought how have $arr[$counter] store actual value of $i.

i have no thought how have $arr[$counter] store actual value of $i.

$counter set 0, $arr[$counter] same saying $arr[0]. if echo $arr[0] you'll see is beingness changed while rest left alone, means code working.

however if want $counter increment also, need tell so:

$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42); $counter=0; for($i=0;$i<7;$i++) { $counter++; $arr[$counter]=$i; } echo $arr[5];

php arrays

No comments:

Post a Comment