Counting unique values in a array

I have


Array
(
    [0] => Array
        (
            [asset_id] => 1
            [room_id] => 1
            [building_id] => 1
        )

    [1] => Array
        (
            [asset_id] => 2
            [room_id] => 1
            [building_id] => 1
        )

    [2] => Array
        (
            [asset_id] => 3
            [room_id] => 1
            [building_id] => 1
        )

    [3] => Array
        (
            [asset_id] => 4
            [room_id] => 1
            [building_id] => 1
        )

)

How can I count the # of unique values of the building_id, so since 1 appears 4 times only 1 would appear…,

Just restructure the array so the building_id is the primary KEY and the asset_id is the secondary key like so.

$assets = array();
foreach($data as $row):
	$assets[$row['building_id']][$row['asset_id']] = $row;
endforeach;

The array now looks like this.

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [asset_id] => 1
                    [room_id] => 1
                    [building_id] => 1
                )

            [2] => Array
                (
                    [asset_id] => 2
                    [room_id] => 1
                    [building_id] => 1
                )

            [3] => Array
                (
                    [asset_id] => 3
                    [room_id] => 1
                    [building_id] => 1
                )

            [4] => Array
                (
                    [asset_id] => 4
                    [room_id] => 1
                    [building_id] => 1
                )

        )

)

You can then just count the assets for the building id… in this case 1.

echo count($assets[1]);

Returns 4.

ok, but im looking at 4 instances of the same building im trying to get 1
Like …

$buildings = array();
foreach($assets as $row):
	//how do I remove duplicates?
endforeach

or some use of array_unique()?

		$buildings = array();
		$name = array();
		foreach($assets as $key=>$value){
		   if(!in_array($value['building_id'], $name)){
			  $name[] = $value['building_id'];
			  $buildings[$key] = $value;
		   }

		}
		

Those `building_ids’ shown in the assets array are not necessarily duplicates but all asset data from $row. You can define the value as just asset_id if you want or just know that $row holds all data related to the asset like maybe a the rack or position or any other field you might add.

It would probably be helpful to add a known key like “assets” as the second key so when you loop through $assets with foreach It only shown building_id once. Within the loop you could then loop through assets if you want.

$assets = array();
foreach($data as $row):
	$assets[$row['building_id']]['assets'][$row['asset_id']] = $row['asset_id'];
endforeach;	

foreach($assets as $building_id => $ar): 
	echo 'Building ID: '.$building_id.'<br >';
	
	echo 'Assets: '; 	
	foreach($assets[$building_id]['assets'] as $asset):	
		//Do what you want with the asset id
		echo $asset;
	endforeach;
	 	 
endforeach;

Resulting in something like

Building ID: 1
Assets: 1234 

AND adding the COUNT as originally asked for might be like this.

$assets = array();
foreach($data as $row):
	$assets[$row['building_id']]['assets'][$row['asset_id']] = $row['asset_id'];
endforeach;	

foreach($assets as $building_id => $ar): 
	echo 'Building ID: '.$building_id.'<br >';
								
	echo 'Asset Cnt.: '.count($assets[$building_id]['assets']).'<br >';
	echo 'Assets: '; 	
	foreach($assets[$building_id]['assets'] as $asset):	
		//Do what you want with the asset id
		echo $asset;
	endforeach;
	 	 
endforeach;
Building ID: 1
Asset Cnt.: 4
Assets: 1234 
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.