0

I have this array

[furnitures] => Array
(
    [0] => Array
    (
        [fi_id] => 8
        [price] => 15.00
    )
    [1] => Array
    (
        [fi_id] => 8
        [price] => 15.00
    )
    [2] => Array
    (
        [fi_id] => 7
        [price] => 15.00
    )
    [3] => Array
    (
        [fi_id] => 7
        [price] => 15.00
    )
    [4] => Array
    (
        [fi_id] => 8
        [price] => 15.00
    )
    [5] => Array
    (
        [fi_id] => 9
        [price] => 15.00
    )
)

I want unique fi_id from it and I also want to count the occurence from it. How many times is this being repreated. Does PHP has any built in function for it?

Like unique items are 7,8,9 and 7 is repreated 2 times, 8 is repreated 3 times and 9 is repreated 1 time.

Ali Zia
  • 3,825
  • 5
  • 29
  • 77

1 Answers1

1

you can try this:

//Here you get unique Ids
$uniqueFiIdList = array_unique(array_map(function ($el) 
{ 
   return $el['fi_id']; 
}, $furnitures));

//Here you get the total unique occurences
$totalUniqueFiId = count($furnitures) - count($uniqueFiIdList);
Vural
  • 8,666
  • 11
  • 40
  • 57