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

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-02-2004
maciej
 
Posts: n/a
Default 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,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
Reply With Quote
  #2 (permalink)  
Old 10-02-2004
Andy Hassall
 
Posts: n/a
Default Re: How to add an element to the middle of an associative array ?

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
Reply With Quote
  #3 (permalink)  
Old 10-04-2004
Shawn Wilson
 
Posts: n/a
Default Re: How to add an element to the middle of an associative array ?

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:54 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0