How to add an element to the middle of an associative array ?

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

    How to add an element to the middle of an associative array ?

    I got an array that consists of elements that are arrays also. Now I
    wish I could add an element to the middle of it. Let mi give you an
    example:

    array (
    - array(1,15,appl e),
    - array(2,28,bana na),
    - array(3,41,oran ge)
    }

    I would like to add, let's say, carrot on 2nd position:

    array (
    - array(1,15,appl e),
    - array(2,57,carr ot),
    - array(3,28,bana na),
    - array(4,41,oran ge)
    }

    Sorry for that kind of presentation, but it's clear i think.

    Array_splice() does not work (or maybe i use it in a wrong way? - it
    splits an adding element for, in that case, 3 elements). Array_push()
    adds only to the end of an array. can samobody tell me how to add and
    element to the middle of an associative array?

    I hope you can understand what i've written here :)

    Thanx,
    maciej
  • Andy Hassall

    #2
    Re: How to add an element to the middle of an associative array ?

    On 2 Oct 2004 10:46:37 -0700, forweb@poczta.f m (maciej) wrote:
    [color=blue]
    >I got an array that consists of elements that are arrays also. Now I
    >wish I could add an element to the middle of it. Let mi give you an
    >example:
    >
    >array (
    >- array(1,15,appl e),
    >- array(2,28,bana na),
    >- array(3,41,oran ge)
    >}
    >
    >I would like to add, let's say, carrot on 2nd position:
    >
    >array (
    >- array(1,15,appl e),
    >- array(2,57,carr ot),
    >- array(3,28,bana na),
    >- array(4,41,oran ge)
    >}
    >
    >Sorry for that kind of presentation, but it's clear i think.
    >
    >Array_splice () does not work (or maybe i use it in a wrong way? - it
    >splits an adding element for, in that case, 3 elements). Array_push()
    >adds only to the end of an array. can samobody tell me how to add and
    >element to the middle of an associative array?[/color]

    Slightly confused because what you've posted isn't an associative array, it's
    a "plain" array of arrays.

    Could you post the output of var_dump(), print_r() or var_export() for your
    before and after arrays? That'd make it clearer.

    If they are as you post them, then array_splice() followed by a loop
    incrementing the first element of the sub-array for the subsequent sub-arrays
    would do it.

    <pre>
    <?php
    $a = array (
    array(1,15,'app le'),
    array(2,28,'ban ana'),
    array(3,41,'ora nge'),
    );

    var_export($a);
    print "<hr>";

    // note the extra array() - to insert a single element
    // which itself is an array
    array_splice($a , 1, 0, array(array(2,5 7,'carrot')));

    for ($i=2; $i<count($a); $i++)
    $a[$i][0]++;

    var_export($a);

    ?>
    </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

    • Shawn Wilson

      #3
      Re: How to add an element to the middle of an associative array ?

      maciej wrote:[color=blue]
      >
      > I got an array that consists of elements that are arrays also. Now I
      > wish I could add an element to the middle of it. Let mi give you an
      > example:
      >
      > array (
      > - array(1,15,appl e),
      > - array(2,28,bana na),
      > - array(3,41,oran ge)
      > }
      >
      > I would like to add, let's say, carrot on 2nd position:
      >
      > array (
      > - array(1,15,appl e),
      > - array(2,57,carr ot),
      > - array(3,28,bana na),
      > - array(4,41,oran ge)
      > }
      >
      > Sorry for that kind of presentation, but it's clear i think.
      >
      > Array_splice() does not work (or maybe i use it in a wrong way? - it
      > splits an adding element for, in that case, 3 elements). Array_push()
      > adds only to the end of an array. can samobody tell me how to add and
      > element to the middle of an associative array?[/color]

      I think this should work, but I haven't tested:

      array_splice ( $yourarray, 1, 0, array(array(2,5 7,carrot)));

      Whatever you're inserting into the array has to be in an array itself.

      Shawn
      --
      Shawn Wilson
      shawn@glassgian t.com

      Comment

      Working...