Jump to content

Array related question


david212

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/200671-array-related-question/
Share on other sites

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"

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.