Replace element in an associtive array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Replace element in an associtive array

Post by Ollie Saunders »

I have an array ($a) that looks like this

Code: Select all

Array
(
    [foo] => 4
    [bar] => 6
    [zim] => 7
)
and I want to replace 'bar' with this array ($new):

Code: Select all

Array
(
    [gir] => 5
    [gaz] => 9
)
producing this:

Code: Select all

Array
(
    [foo] => 4
    [gir] => 5
    [gaz] => 9
    [zim] => 7
)
As you can see need to preserve the order.

Either of these can do the replacement:

Code: Select all

$b = array_merge(array_slice($a, 0, 1), $new, array_slice($a, 2)); 
// or
$b = array_splice($a, 1, 1, $new);
But require me to specify an integer array position. As I don't know the position of the element I want to replace, this is a problem. Short of foreaching through the array incrementing a counter, getting such an integer (the internal array position of a associative key) seems impossible.

Help!
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

Your going to have to make condition, because if key is at [0], then you don't slice array [0] in your array_merge, you use a empty array. The same goes if [bar] is the last element in your array, you would then just return $add! But I think it would be better to just use a foreach()

Code: Select all

<?php

$array = array ( 'foo' => 4, 'bar' => 6, 'zim' => 7 );

$add = array ( 'gir' => 5, 'gaz' => 9 );

$out = array ();

foreach ( $array AS $ok => $ov )
{
	if ( $ok == 'bar' )
	{
		foreach ( $add AS $nk => $nv )
		{
			$out[$nk] = $nv;
		}
	}
	else
	{
		$out[$ok] = $ov;
	}
}

print_r ( $out );


?>
If you want to do it the other way...

Code: Select all

<?php

$array = array ( 'foo' => 4, 'bar' => 6, 'zim' => 7 );

$add = array ( 'gir' => 5, 'gaz' => 9 );

$key = array_search ( 'bar', array_keys ( $array ) );

if ( $key == 0 )
{
	$out = array_merge ( array (), $add, array_slice ( $array, 1 ) );
}
else if ( $key >= 1 && $key < ( sizeof ( $array ) - 1 )  )
{
	$out = array_merge (array_slice ( $array, 0, $key ), $add, array_slice ( $array, ( $key + 1 ) ) );
}
else
{
	$out = $add;
}


print_r ( $out );


?>

printf
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Thanks printf. I've decided to make the array have integer keys, use array_splice and keep a reference of the original keys in a separate array.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could do:

Code: Select all

$keys = array_keys($myArray);
$index = array_search('bar',$keys);
array_splice($myArray,$index,1,$myReplacementArray);
Untested, but it should work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply