note 79450 added to ref.array
| From: | ashwin dot date at tekdi dot net | Date: | Wed, 28 Nov 2007 09:22:01 +0000 |
| Subject: | note 79450 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132612@lists.php.net to get a copy of this message | ||
This function will multiply all elements in an array with a number or float you specify. You can
also multiply elements in an array with corresponding elements in another array. I have used the
index checker script by Nicolai Bloch.
<?php
//Check for numeric array
function isNumeric($ar) {
$keys = array_keys($ar);
natsort($keys); //String keys will be last
return is_int(array_pop($keys));
}
//Check for associative array
function isAssoc($ar) {
$keys = array_keys($ar);
natsort($keys); //Numeric keys will be first
return is_string(array_shift($keys));
}
// Multiply elements of $array
// @arg $array is the array to perform multiplication on
// @arg $input - If it is a number, all elements from $array are multipled by $input
// If $input is an array, elements in $array are multiplied by corresponding elements $input
//
function array_multiplier($array, $input, $maintain_index = true) {
if (is_array($input)) {
$array_values = array_values($array);
$array_keys = array_keys($array);
$input_values = array_values($input);
if (count($array_values) == count($input_values)) {
for ($i=0;$i<=(count($array_values));$i++) {
if (isAssoc($array) && $maintain_index) {
$return->$array_keys[$i] = $array_values[$i] * $input_values[$i];
} else {
$return[$array_keys[$i]] = $array_values[$i] * $input_values[$i];
}
}
} else {
$return = 'Both arrays need to be of same size';
}
} elseif (is_float($input) || is_int($input)) {
foreach ($array as $k=>$v) {
$return->$k = $v * $input;
}
} else {
$return = 'Multiplier not defined';
}
return $return;
}
?>
----
Server IP: 203.199.69.133
Probable Submitter: 59.162.93.244
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/79450
Del: integrated -- https://master.php.net/note/delete/79450/integrated
Del: useless -- https://master.php.net/note/delete/79450/useless
Del: bad code -- https://master.php.net/note/delete/79450/bad+code
Del: spam -- https://master.php.net/note/delete/79450/spam
Del: non-english -- https://master.php.net/note/delete/79450/non-english
Del: in docs -- https://master.php.net/note/delete/79450/in+docs
Del: other reasons-- https://master.php.net/note/delete/79450
Reject -- https://master.php.net/note/reject/79450
Search -- https://master.php.net/manage/user-notes.php