Following is the array and I want count of if product
name
is Powerwall
Array
(
[0] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[1] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[2] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[3] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[4] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[5] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[6] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[7] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[8] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Model S
[category] => product
)
)
)
Output needed 8 for Powerwall and 1 for Model S
You need to loop through your array and increment the counter $count
every time Powerwall is found.
$count = 0;
foreach ($array as $arr) {
if ($arr['product']['name'] == 'Powerwall') {
$count++;
}
}
echo "Powerwall count = " . $count;
Just use array_column
and array_count_values
for counting the no
<?php
$dataarray=Array
(
0 => Array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => Array
(
"name" => "Powerwall",
"category" => "product",
)
),
1 => Array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
2 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
3 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
4 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
5 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
6 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
7 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
8 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Model S",
"category" => "product",
)
),
);
$result= array_count_values(array_column(array_column($dataarray,"product"),"name"));
echo "Count: ".$result["Powerwall"];