Jump to content

How do I count values out of a multi-dimensional array


vincej

Recommended Posts

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 !!

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.