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;