0

I have following array.

array(5) {
  [0]=>
  array(1) {
    ["Cars"]=>
    string(5) "Volvo"
  }
  [1]=>
  array(1) {
    ["Cars"]=>
    string(4) "Fiat"
  }
  [2]=>
  array(1) {
    ["Cars"]=>
    string(5) "Volvo"
  }
  [3]=>
  array(1) {
    ["Cars"]=>
    string(8) "Mercedes"
  }
  [4]=>
  array(1) {
    ["Cars"]=>
    string(5) "Volvo"
  }

I need to count all Duplicates and create a new array where i have the name of each group and the number how many duplicates there are. Could someone help me with a simple solution?

7
  • 3
    Have you made any attempts of solving this yourself? Commented Mar 29, 2019 at 13:55
  • 1
    SO is not a coding service - what have you tried? Commented Mar 29, 2019 at 13:55
  • 1
    Please go read How to Ask. We can help, after you have shown us what you have tried already, and properly described what your issues with that were. We are not here to provide copy&paste-ready solutions for people who haven’t even lifted a finger yet though. Commented Mar 29, 2019 at 13:59
  • Ive got my sql-result where i loop through to create the array. My plan was to create a multiple array with the name and the total number (count) of that name. This did not worked how i planend it so i started over Commented Mar 29, 2019 at 14:00
  • Hint: take a look at array_column and array_count_values Commented Mar 29, 2019 at 14:00

1 Answer 1

1

Find all values of Carswith array_column(), and count their values with array_count_values().

$array = array(
    ['Cars' => 'Volvo'],
    ['Cars' => 'Fiat'],
    ['Cars' => 'Volvo'],
    ['Cars' => 'Mercedes'],
    ['Cars' => 'Volvo'],
    );

print_r(array_count_values(array_column($array, "Cars")));

Outputs

Array (
    [Volvo] => 3
    [Fiat] => 1
    [Mercedes] => 1
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.