This is a discussion on How to add an element to the middle of an associative array ? within the PHP Language forums, part of the PHP Programming Forums category; I got an array that consists of elements that are arrays also. Now I wish I could add an element ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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,apple), - array(2,28,banana), - array(3,41,orange) } I would like to add, let's say, carrot on 2nd position: array ( - array(1,15,apple), - array(2,57,carrot), - array(3,28,banana), - array(4,41,orange) } 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 |
|
|||
|
On 2 Oct 2004 10:46:37 -0700, forweb@poczta.fm (maciej) wrote:
>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,apple), >- array(2,28,banana), >- array(3,41,orange) >} > >I would like to add, let's say, carrot on 2nd position: > >array ( >- array(1,15,apple), >- array(2,57,carrot), >- array(3,28,banana), >- array(4,41,orange) >} > >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? 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,'apple'), array(2,28,'banana'), array(3,41,'orange'), ); 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,57,'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.andyhsoftware.co.uk/space> Space: disk usage analysis tool |
|
|||
|
maciej wrote:
> > 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,apple), > - array(2,28,banana), > - array(3,41,orange) > } > > I would like to add, let's say, carrot on 2nd position: > > array ( > - array(1,15,apple), > - array(2,57,carrot), > - array(3,28,banana), > - array(4,41,orange) > } > > 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 think this should work, but I haven't tested: array_splice ( $yourarray, 1, 0, array(array(2,57,carrot))); Whatever you're inserting into the array has to be in an array itself. Shawn -- Shawn Wilson shawn@glassgiant.com http://www.glassgiant.com |