note 101407 added to function.array-count-values

From: Date: Wed, 15 Dec 2010 08:56:43 +0000
Subject: note 101407 added to function.array-count-values
Groups: php.notes 
Request: Send a blank email to php-notes+get-174935@lists.php.net to get a copy of this message
Hy, i had a problem which was associated with the array_count_values function, but did not solve my problem realy well so i made a function by my self and maybe it can help also someone else. The problem was that i had a table like this: CL PRODNUM QN DATE 1 4221110030 2 10.12.2010 2 4221110031 1 10.12.2010 3 5241110002 1 10.12.2010 3 5241110003 1 11.12.2010 3 5241110001 2 11.12.2010 3 6241110001 1 11.12.2010 1 2231110999 1 10.12.2010 1 2231110999 1 11.12.2010 (it go's on and on ... ) And i wanted to calculate for every unique date how much the quantity of the specific CL is. For example the result should be like this: 10.12.2010: [1] -> 3 10.12.2010: [2] -> 1 10.12.2010: [3] -> 1 11.12.2010: [1] -> 1 11.12.2010: [2] -> 0 11.12.2010: [3] -> 4 So, i had to make a function which should look for unique values of a date and then for that date get the quantity of uniqe CL's. For note: i put all the values of the table into an array: <?php $a = array(); $a['cl'] = 1; $a['prodnum'] = '4221110030'; $a['qn'] = '2'; $a['date'] = '10.12.2010'; ?> The function looks like this: <?php function csav( $array, $distinctV, $searchInV, $sumValue ){ #result array $result = array(); #finding the unique dates $array1 = array_unique($array[$searchInV]); $array2 = array(); $unique1 = array_merge($array2, $array1); #finding the unique CL's $array3 = array_unique($array[$distinctV]); $array4 = array(); $unique2 = array_merge($array4, $array3); #for statements through the unique dates for($k = 0; $k < count($unique1); $k++){ for($i = 0; $i < count($unique2); $i++){ $sum = 0; for($j = 0; $j < count($array[$distinctV]); $j++){ if($array[$distinctV][$j] == $unique2[$i] and $array[$searchInV][$j] == $unique1[$k]) $sum += $array[$sumValue][$j]; } $result[$unique1[$k]][$unique2[$i]] = $sum; } } return $result; } ?> The use of function has to be like this: <?php $result = csav($a, 'cl', 'date', 'qn'); print_r($result); ?> And the result looks like this: Array ( [2010-12-10] => Array ( [1] => 14 [2] => 17 [3] => 4 ) [2010-12-13] => Array ( [1] => 13 [2] => 17 [3] => 4 ) [2010-12-14] => Array ( [1] => 13 [2] => 17 [3] => 4 ) [2010-12-15] => Array ( [1] => 14 [2] => 17 [3] => 4 ) [2010-12-16] => Array ( [1] => 14 [2] => 17 [3] => 2 ) ) This is it. Hope it helps someone. ---- Server IP: 91.185.195.14 Probable Submitter: 212.18.38.37 ---- Manual Page -- http://www.php.net/manual/en/function.array-count-values.php Edit -- https://master.php.net/note/edit/101407 Del: integrated -- https://master.php.net/note/delete/101407/integrated Del: useless -- https://master.php.net/note/delete/101407/useless Del: bad code -- https://master.php.net/note/delete/101407/bad+code Del: spam -- https://master.php.net/note/delete/101407/spam Del: non-english -- https://master.php.net/note/delete/101407/non-english Del: in docs -- https://master.php.net/note/delete/101407/in+docs Del: other reasons-- https://master.php.net/note/delete/101407 Reject -- https://master.php.net/note/reject/101407 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174935) next »