david212 Posted May 4, 2010 Share Posted May 4, 2010 Hello there. I have this php array code: <?php $xR = array(3 => "ar", "br", 6 => "cr", "de", 10 => "en", "fr"); $xR[8] = "ge"; $xR[] = "hr"; ?> what would be the key for "br"? I think it's 4. But when i want to know the value for "hr" using print_r($xR); i'm getting 12 for "hr" . If the last $xR[8] = "ge"; the "hr" won't be the 9? :S by assingning the next increment after used number? Please explain, im confused with this one Thank you Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted May 4, 2010 Share Posted May 4, 2010 To return arrays with indexes, use somethign like print_r($array), or var_dump($array); I'm sure you will get your answer by looking at the arrays them selves. -cb- Quote Link to comment Share on other sites More sharing options...
david212 Posted May 4, 2010 Author Share Posted May 4, 2010 i've mentioned that i used print_r($array) an got the 12 but why ? Because the last increment stopped at 11? But i'm asking, if the $xR[8] = "ge, for the "hr" won't be the 9, i mean based on the "8" value incrementing only 8 not 11 Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2010 Share Posted May 4, 2010 Breaking it down: $xR = array(3 => "ar", "br", 6 => "cr", "de", 10 => "en", "fr"); That gives this: [3] => "ar" [4] => "br" [6] => "cr" [7] => "de" [10] => "en" [11] => "fr" $xR[8] = "ge"; That says position at 8 will be "ge": [3] => "ar" [4] => "br" [6] => "cr" [7] => "de" [10] => "en" [11] => "fr" [8] => "ge" $xR[] = "hr"; Since the last index stopped at 11 when you created the array, the next one is 12, not 9. $xR[8] = "ge"; doesn't do any increment. It just says to store the value of "ge" at index 8. You can run $xR[8] = "ge"; 100 times and it won't change the index because that's just replacing a value. [3] => "ar" [4] => "br" [6] => "cr" [7] => "de" [10] => "en" [11] => "fr" [8] => "ge" [12] => "hr" Quote Link to comment Share on other sites More sharing options...
david212 Posted May 4, 2010 Author Share Posted May 4, 2010 Thank you very much now i understand better ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.