arrays

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

    arrays

    I am reading the php manual online and don't understand the way arrays work.
    It says arrays take a key, value pair which is fine I guess, but how do I
    get an old fashion c style array? Are the keys like the indices?
    say I want an array that contains the numbers 0 through 10, what do I make
    the keys or does it even matter?

    Thanx,
    Chris


  • steve

    #2
    Re: arrays

    "Christophe r9" wrote:[color=blue]
    > I am reading the php manual online and don’t understand the way
    > arrays work.
    > It says arrays take a key, value pair which is fine I guess, but[/color]
    how[color=blue]
    > do I
    > get an old fashion c style array? Are the keys like the indices?
    > say I want an array that contains the numbers 0 through 10, what do[/color]
    I[color=blue]
    > make
    > the keys or does it even matter?
    >
    > Thanx,
    > Chris[/color]

    Chris, php is easy going, and at the same time, great in managing
    arrays. Compared to c, most of the hard work is done for you.

    So you can say
    $a = array (’x’, ’y’, 33);
    then $a[0] = ’x’ and $a[1] = ’y’ and $a[2] = 33
    as you see, phy is just incrementing the key as it is assigning. You
    can also mix types-as php is very forgiving.

    Now to add another item to the array, simply do:
    $a[] = 44;
    in this case $a[4] = 44

    you can also set the keys to be whatever you want, e.g.
    $b = array( ’key1’ => 22, ’key2’ => 55);
    then $b[’key1’] = 22 ...
    Also php will assign incrementing keys - in addition to the defined
    keys.
    So $b[0] = 22 as well.

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/PHP-arrays-ftopict130390.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=435003

    Comment

    • Michael Fesser

      #3
      Re: arrays

      .oO(steve)
      [color=blue]
      >you can also set the keys to be whatever you want, e.g.
      >$b = array( ’key1’ => 22, ’key2’ => 55);[/color]

      It's called an associative array, where the keys are strings. But in
      fact there's no real difference between indexed and associative arrays,
      in PHP every array is like a hashtable.
      [color=blue]
      >then $b[’key1’] = 22 ...
      >Also php will assign incrementing keys - in addition to the defined
      >keys.
      >So $b[0] = 22 as well.[/color]

      $b = array('key1' => 22, 'key2' => 55);
      print $b[0];

      --> Notice: Undefined offset: 0 ...

      $b[] = 42;
      print_r($b);

      --> Array
      (
      [key1] => 22
      [key2] => 55
      [0] => 42
      )

      Micha

      Comment

      • steve

        #4
        Re: Re: arrays

        "Michael Fesser" wrote:[color=blue]
        > .oO(steve)
        >[color=green]
        > >you can also set the keys to be whatever you want, e.g.
        > >$b = array( ’key1’ => 22, ’key2’ => 55);[/color]
        >
        > It’s called an associative array, where the keys are strings.
        > But in
        > fact there’s no real difference between indexed and associative
        > arrays,
        > in PHP every array is like a hashtable.
        >[color=green]
        > >then $b[’key1’] = 22 ...
        > >Also php will assign incrementing keys - in addition to the[/color][/color]
        defined[color=blue][color=green]
        > >keys.
        > >So $b[0] = 22 as well.[/color]
        >
        > $b = array(’key1’ => 22, ’key2’ => 55);
        > print $b[0];
        >
        > --> Notice: Undefined offset: 0 ...
        >
        > $b[] = 42;
        > print_r($b);
        >
        > --> Array
        > (
        > [key1] => 22
        > [key2] => 55
        > [0] => 42
        > )
        >
        > Micha[/color]

        Michael is correct, my mistake.
        $b = array(’key1’ => 22, ’key2’ => 55);
        print $b[0];

        The keys assigned are ’key1’ and ’key2’ and there is no
        auto-incremting of indecis starting with zero. So $b[0] IS undefined.

        --
        http://www.dbForumz.com/ This article was posted by author's request
        Articles individually checked for conformance to usenet standards
        Topic URL: http://www.dbForumz.com/PHP-arrays-ftopict130390.html
        Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=435420

        Comment

        Working...