By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,678 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,678 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2 Oct 2004 10:46:37 -0700, fo****@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
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
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.