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
Share on other sites

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

Link to comment
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"

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.