note 22605 modified in function.array-search by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:49:17 +0000 |
| Subject: | note 22605 modified in function.array-search by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79831@lists.php.net to get a copy of this message | ||
Here's how you can use array_search() to replace all occurances of a value in an array:
<?php
function array_replace($a, $tofind, $toreplace)
{
$i = array_search($tofind, $a);
if ($i === false)
{
return $a;
}
else
{
$a[$i] = $toreplace;
return array_replace($a, $tofind, $toreplace);
}
}
?>
Usage:
$a = array(1,2,3);
$a = array_replace($a, 1, 4);
echo $a[0]; // Outputs 4
--was--
Here's how you can use array_search() to replace all occurances of a value in an array:
function array_replace($a, $tofind, $toreplace)
{
$i = array_search($tofind, $a);
if ($i === false)
{
return $a;
}
else
{
$a[$i] = $toreplace;
return array_replace($a, $tofind, $toreplace);
}
}
Usage:
$a = array(1,2,3);
$a = array_replace($a, 1, 4);
echo $a[0]; // Outputs 4
http://php.net/manual/en/function.array-search.php