note 79449 added to ref.array

From: Date: Wed, 28 Nov 2007 09:21:12 +0000
Subject: note 79449 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-132611@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/79449 Del: integrated -- https://master.php.net/note/delete/79449/integrated Del: useless -- https://master.php.net/note/delete/79449/useless Del: bad code -- https://master.php.net/note/delete/79449/bad+code Del: spam -- https://master.php.net/note/delete/79449/spam Del: non-english -- https://master.php.net/note/delete/79449/non-english Del: in docs -- https://master.php.net/note/delete/79449/in+docs Del: other reasons-- https://master.php.net/note/delete/79449 Reject -- https://master.php.net/note/reject/79449 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132611) next »