Counting how many elements in array

I have the connection here and the query allright, but I can’t count how many of each there is in the array. Like 27 sedans, and 3 Vans and so on…

			$url = 'https://gw.bilinfo.net/listingapi/api/export';
			// provide your username and password here
			$auth = base64_encode("demo:ocfB6XzF73");
			// create HTTP context with basic auth
			$context = stream_context_create([
			'http' => ['header' => "Authorization: Basic $auth"]
			]);
			    // query for data
			    $data = file_get_contents($url, false, $context);
			    // $escaped = json_encode($data);
			    $escaped = json_decode($data);

			$iTypeGet = $escaped->Vehicles->Type;

			$iTypeCount = array_count_values($iTypeGet);
			//Counting how many of each element in array there are! But is not working...
			foreach ($escaped->Vehicles as $vehicle) {
			    $temp .= "<option class='optionOne' value='" . $vehicle->Type . "'>" . $vehicle->Type . " (" . $iTypeCount . ")</option>";
			}

Try count($iTypeGet); instead of array_count_values()…

Hi @ErnieAlex , Have tried that with no luck… :face_vomiting: :joy:, only got to number of all the elements

Well, it depends on your data structure how it needs to be counted. You might want to debug the actual data inside of iTypeGet. You can do this at the point you want to count them:

die("<pre>".print_r($iTypeGet,1)."</pre>");

What this will do stop the processing at that point and display the full data structure of what is really inside of that variable. Then, you can see what is needed to be able to count the entries. I suspect that you have data inside of it that is oddly formed. Also, you are using JSON decoded values. You did create a JSON object, you did not create an array, but, an object. Therefore, you would need to know the top level value in the object. Then, count that. Something loosely like:

$iTypeCount = count($iTypeGet['level-one-name']);

You would need to know the top level of the structure and place that into the count array pointer.
Make sense? Good luck !

@ErnieAlex I tried this:
$iTypeGetArray = $escaped->Vehicles;
foreach ($iTypeGetArray as $CountArray) {
$temp .= “” . $vehicle->Type . " (" . count($CountArray->Type) . “)”;
}

Hi there,

this might come as a late response, maybe very late, but I think this is what you are actually looking for:

$url = 'https://gw.bilinfo.net/listingapi/api/export';
// provide your username and password here
$auth = base64_encode("demo:ocfB6XzF73");
// create HTTP context with basic auth
$context = stream_context_create([
    'http' => ['header' => "Authorization: Basic $auth"]	
]);
// query for data
$data = file_get_contents($url, false, $context);
// $escaped = json_encode($data);
$escaped = json_decode($data);

$iTypeGet = $escaped->Vehicles;

$typesTogether = [];
$temp = '';


foreach($iTypeGet as $vehicle) {
    if(!array_key_exists($vehicle->Type, $typesTogether)) {
        $typesTogether[$vehicle->Type] = 0;
    } else {
        $typesTogether[$vehicle->Type] += 1;
    }
}


foreach($typesTogether as $key => $value) {
    $temp .= "<option class='optionOne' value='$key'>$key ($value) </option>";
}

Please let me know if this helped you :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service