php arrays!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raashid bhatt

    #1

    php arrays!

    as we know if we dont ptovide the key in array by default it uses 0
    and n + 1 for keys
    example

    $arr = array("raashid" , "aslam", "bhatt");

    it is same as
    $arr = array (0 ="raashid", 1 =>............ , 2 =>....);

    and another example
    $arr = array("raashid" , 2 ="aslam", "bhatt");
    0 2 3

    the value raashid will automatically get value 0
    while as aslam stays at 2 (note we can rearrange them using
    array_vaules() function)

    bhatt will automatically get 3 because as i stated early n + 1 for
    keys

    that was just an intro my main point is

    $arr = array("raashid" , "2" ="aslam", "bhatt");

    here the key("2") is a string and it must not use n + 1

    bhatt's key should be 1 as 0 + 1 = 1

    now

    var_dump($arr);
    array(3) { [0]= string(7) "raashid" [2]= string(5) "aslam" [3]=>
    string(5) "bhatt" }

    see the key for raashid is converted into integer but it was not

    and bhatt gets key 3 thst is previous + 1
  • Sjoerd

    #2
    Re: php arrays!

    On Aug 14, 8:52 am, raashid bhatt <raashid_...@ya hoo.comwrote:
    $arr = array("raashid" , "2" ="aslam", "bhatt");
    >
    here the key("2") is a string and it must not use n + 1
    >
    bhatt's key should be 1 as 0 + 1 = 1
    You are basically right, but the string "2" is converted to the
    integer 2 already by array():

    $arr = array("1" ="one");
    var_dump($arr);

    gives
    [1] =string(3) "one"

    instead of
    ["1"] =string(3) "one"

    Comment

    Working...