note 101407 deleted from function.array-count-values by googleguy

From: Date: Tue, 22 Oct 2013 06:50:05 +0000
Subject: note 101407 deleted from function.array-count-values by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196663@lists.php.net to get a copy of this message
Note Submitter: muhamed dot pasha at gmail dot com ---- 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.

« previous php.notes (#196663) next »