insert an element in numbered array

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

    insert an element in numbered array

    Hello,


    I need to do the following:

    Say I have a multidim array:

    $array = array();

    $array[0]['name'] = 'Kevin';
    $array[0]['age'] = 23;

    $array[1]['name'] = 'Julia';
    $array[1]['age'] = 31;

    $array[2]['name'] = 'Bob';
    $array[2]['age'] = 26;

    $array[3]['name'] = 'Drew';
    $array[3]['age'] = 37;

    and I have a seperate array:

    $person = array();
    $person['name'] = 'Dana';
    $person['age'] = 33;

    now I want to insert the $person array into the $array in such a way
    that it ends up at index 1 and the subsequent elements get pushed up and
    renumbered, so $array[1] becomes $array[2] and so on.

    Does php have a built-in function that does this?


    ..soma
  • Ewoud Dronkert

    #2
    Re: insert an element in numbered array

    On Wed, 01 Jun 2005 13:29:41 +0200, somaboy mx wrote:[color=blue]
    > now I want to insert the $person array into the $array in such a way
    > that it ends up at index 1 and the subsequent elements get pushed up and
    > renumbered, so $array[1] becomes $array[2] and so on.[/color]

    No one built-in function, but you could use array_slice and array_merge:

    $p = array( 'name' => 'John', 'age' => 42 );
    $insertat = 1;

    if ( $insertat <= 0 )
    {
    $a = array_merge( array( $p ), $a );
    }
    elseif ( $insertat >= count( $a ) )
    {
    $a[] = $p;
    }
    else
    {
    $a0 = array_slice( $a, 0, $insertat );
    $a1 = array_slice( $a, $insertat );
    $a = array_merge( $a0, array( $p ), $a1 );
    }


    --
    Firefox Web Browser - Rediscover the web - http://getffox.com/
    Thunderbird E-mail and Newsgroups - http://gettbird.com/

    Comment

    • Andy Hassall

      #3
      Re: insert an element in numbered array

      On Wed, 01 Jun 2005 13:29:41 +0200, somaboy mx <nosuch@fakemai l.fk> wrote:
      [color=blue]
      >Say I have a multidim array:
      >
      >$array = array();
      >
      >$array[0]['name'] = 'Kevin';
      >$array[0]['age'] = 23;[/color]
      [snip]
      [color=blue]
      >and I have a seperate array:
      >
      >$person = array();
      >$person['name'] = 'Dana';
      >$person['age'] = 33;
      >
      >now I want to insert the $person array into the $array in such a way
      >that it ends up at index 1 and the subsequent elements get pushed up and
      >renumbered, so $array[1] becomes $array[2] and so on.
      >
      >Does php have a built-in function that does this?[/color]

      Yes, array_splice.

      <pre>
      <?php
      $array = array();

      $array[0]['name'] = 'Kevin';
      $array[0]['age'] = 23;

      $array[1]['name'] = 'Julia';
      $array[1]['age'] = 31;

      $array[2]['name'] = 'Bob';
      $array[2]['age'] = 26;

      $array[3]['name'] = 'Drew';
      $array[3]['age'] = 37;

      $person = array();
      $person['name'] = 'Dana';
      $person['age'] = 33;

      print_r($array) ;
      print_r($person );

      array_splice($a rray, 1, 0, array($person)) ;

      print_r($array) ;
      ?>
      </pre>

      --
      Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

      Comment

      • Ewoud Dronkert

        #4
        Re: insert an element in numbered array

        On Wed, 01 Jun 2005 13:42:15 +0100, Andy Hassall wrote:[color=blue]
        > Yes, array_splice.[/color]

        Ah damn, overlooked :)


        --
        Firefox Web Browser - Rediscover the web - http://getffox.com/
        Thunderbird E-mail and Newsgroups - http://gettbird.com/

        Comment

        • somaboy mx

          #5
          Re: insert an element in numbered array

          Andy Hassall wrote:
          [color=blue]
          > Yes, array_splice.[/color]

          Thanks! I was afraid that array_splice would merely replace the element
          at the given index but obviously it works.

          thanks again


          ..soma

          Comment

          Working...