vincej Posted April 13, 2012 Share Posted April 13, 2012 HI ! I have a table which captures how customers came to be customer ie 'advertisement', 'friend' etc etc. So I want to be able to show / count that N number came from referral or advertisement. My query is simple, I select all from the Media column. This gives me a multi-dimensional array, where each table row, becomes a new sub array. I have 3000 customers ... that's a quite a few sub arrays ! So, a sample print_r of the array looks like this: Array ( [0] => Array ( [media] => friend ) [1] => Array ( [media] => friend ) [2] => Array ( [media] => friend ) [3] => Array ( [media] => Coming Over From Old Site I have tried using a couple of array functions such as count(array,1) and (array_count_values($media)) but they both don't count the individual values out of the sub arrays. I'm a bit stuck - Can anyone help me ? Many Many Thanks !! Quote Link to comment Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 Why wouldn't you count this value in the SQL query? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted April 13, 2012 Share Posted April 13, 2012 SELECT media, COUNT(*) AS customer_count FROM customers GROUP BY media Quote Link to comment Share on other sites More sharing options...
TimeBomb Posted April 13, 2012 Share Posted April 13, 2012 You could use PHP's array_walk_recursive. $array = array() // sql data in here $types = array('advertisement' => 0, 'friend' => 0, 'other' => 0); function processArray($value, $key) { if ($value == 'bla') { $types['advertisement']++; } elseif ($value == 'blabla') { $types['friend']++; } else { $types['other']++; } } array_walk_recursive($array, 'processArray'); Quote Link to comment Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 Why not avoid the single-use function and use a foreach? <?php $counts = array(); foreach( $sql_data as $data ) if( isset($data['media']) ) { if( isset($counts[$data['media']]) ) $counts[$data['media']]++; else $counts[$data['media']] = 1; } ?> Regardless, this is going to get ugly with the amount of rows he's dealing with. Andy-H's solution is ideal. Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted April 13, 2012 Share Posted April 13, 2012 You could use PHP's array_walk_recursive. $array = array() // sql data in here $types = array('advertisement' => 0, 'friend' => 0, 'other' => 0); function processArray($value, $key) { if ($value == 'bla') { $types['advertisement']++; } elseif ($value == 'blabla') { $types['friend']++; } else { $types['other']++; } } array_walk_recursive($array, 'processArray'); Probably the easiest way is what TimeBomb suggested, but here is another look at it <?php $array = Array ( 0 => Array ( 'media' => 'friend' ), 1 => Array ( 'media' => 'friend' ), 2 => Array ( 'media' => 'friend' ), 3 => Array ( 'media' => 'Coming Over From Old Site' ) ); $second = array(); foreach ($array as $val) { foreach ($val as $nval) { $second[] = $nval; } } print_r(array_count_values($second)); Quote Link to comment Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 Think about the logic. You're looping through an array to build another array, only to have it looped through again. Look at my post. If it can't be done via SQL, loop through the array once and determine your counts. Why add a second loop or single-use function into the mix? BTW - You have a typo in your signature. Quote Link to comment Share on other sites More sharing options...
vincej Posted April 13, 2012 Author Share Posted April 13, 2012 Thanks guys, I'll try the group by in sql - I feel stupid I had not thought of that one first ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.