How to add element in php array dynamically?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kaiwing18@hotmail.com

    How to add element in php array dynamically?

    HI

    I want to add two element in a array. the first one is
    the key and the second is the value.but I want to add it
    dynamically. i want to do it like the following code:
    $arr="";
    for($i=0;$i<20; $i++) {

    $arr[$i]=arr($i=>$i+1);

    array_push($arr[$i]);

    }

    print_r($arr);

    But of couse it don't work. Could anyone tell me how
    to do it ?


    Thx

    Ricky
  • Andrew Boothman

    #2
    Re: How to add element in php array dynamically?

    kaiwing18@hotma il.com wrote:
    [color=blue]
    > I want to add two element in a array. the first one is
    > the key and the second is the value.but I want to add it
    > dynamically. i want to do it like the following code:
    > $arr="";
    > for($i=0;$i<20; $i++) {
    >
    > $arr[$i]=arr($i=>$i+1);
    >
    > array_push($arr[$i]);
    >
    > }
    >
    > print_r($arr);
    >
    > But of couse it don't work. Could anyone tell me how
    > to do it ?[/color]

    I'm no expert (see my own question!) but I think you would do this as:

    for($i=0;$i<20; $i++) {

    array_merge($ar r, array($i => $i+1)}

    }

    In other words you merge your existing array with a new one consisting
    of the new key=>value mapping that you want to add.

    Or at least that's how I've done it before...

    Andrew

    Comment

    • rush

      #3
      Re: How to add element in php array dynamically?

      <kaiwing18@hotm ail.com> wrote in message
      news:d82f3f5c.0 408012036.5a2a0 419@posting.goo gle.com...[color=blue]
      > HI
      >
      > I want to add two element in a array. the first one is
      > the key and the second is the value.but I want to add it
      > dynamically. i want to do it like the following code:[/color]

      $myArray = array( ); // initialize as empty array;

      $myArray[] = 77; //add element 77
      $myArray[] = 88; //add element 88

      $myArray['newKey'] = 'valueForNewKey ';

      rush
      --




      Comment

      • Michael Fesser

        #4
        Re: How to add element in php array dynamically?

        .oO(kaiwing18@h otmail.com)
        [color=blue]
        >I want to add two element in a array. the first one is
        >the key and the second is the value.but I want to add it
        >dynamically. i want to do it like the following code:
        >$arr="";
        >for($i=0;$i<20 ;$i++) {
        >
        > $arr[$i]=arr($i=>$i+1);
        >
        > array_push($arr[$i]);
        >
        >}[/color]

        Not really sure what mean. Could you explain or post an example of what
        $arr should look like after the above loop?


        I would simply interpret your code as

        $arr = array();
        for ($i = 0; $i < 20; $i++) {
        $arr[] = array($i => $i+1);
        }

        but that's probably not what you wanna do.

        Micha

        Comment

        Working...