Reputation: 147
So I have the following array like this :
[0] => Array
(
[id] => 10
[rec_id] => 4
[mid] => a:2:{i:0;s:1:"2";i:1;s:1:"5";}
[cid] => 5
[lang] => geo
[title] => ჯიმი მორისონი გაცოცხლდა
[intro] =>
[img] => xhdvlmvogeujm76.jpg
[text] =>
)
[1] => Array
(
[id] => 7
[rec_id] => 6
[mid] => a:2:{i:0;s:1:"2";i:1;s:1:"5";}
[cid] => 3
[lang] => geo
[title] => 70 people died
[intro] =>
[img] => as554ghbvwe5.jpg
[text] =>
)
)
What I want to do is count how many different Cid (category id) are in my items. Let's say I have 5 items with each a different cid, then counter should return 5, if 3 out of these 5 have the same cid, then it returns 3 and so on. I've been tempering with foreach()
but no luck . here what i tried
foreach ($data as $k=>$v)
{
if ($v['cid']!=$v['cid']){
$counter=$counter+1;
}
}
Upvotes: 0
Views: 56
Reputation: 212502
If you're using PHP 5.5+ you can use
$uniqueCids = array_unique(
array_column(
$myArray,
'cid'
)
);
For PHP < 5.5, you can use
$uniqueCids = array_unique(
array_map(
function($value) {
return $value['cid'];
},
$myArray
)
);
Then just do
$uniqueCidCount = count($uniqueCids);
Upvotes: 5