PDA

View Full Version : problems with array_search()


Antivirus
05-07-2010, 07:32 PM
Ok this is driving me nuts... all common sense tells me this should work however it does not.


$oldname = 'Tiger';
$newname = 'TigerXXX';

$options = array(
[0] => 'Cat'
[1] => 'Cow'
[2] => 'Monkey'
[3] => 'Snake'
[4] => 'Tiger'
[5] => 'Zebra'
);

if($key = array_search($oldname, $options))
{
$options[$key] = trim(unhtmlspecialchars($newname));
asort($options);
}



By all accounts, 'Tiger' should be replaced with 'TigerXXX'... shouldn't it? This is what I am expecting but it's not working out. Seems that array_search() is returning false.

Anyone know why?

Deceptor
05-13-2010, 01:52 PM
You're array isn't exactly a valid array, use:
$options = array(
0 => 'Cat',
1 => 'Cow',
2 => 'Monkey',
3 => 'Snake',
4 => 'Tiger',
5 => 'Zebra',
);

Antivirus
05-13-2010, 02:35 PM
My bad I didn't properly write the array. It is actually Valid as you show since it's created by using explode().