note 22605 deleted from function.array-search by levim

From: Date: Mon, 24 Feb 2014 22:47:29 +0000
Subject: note 22605 deleted from function.array-search by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198667@lists.php.net to get a copy of this message
Note Submitter: saltymeat at hotmail dot com ---- 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

« previous php.notes (#198667) next »