0

I'm hoping someone can help.

I have values in an array.. Let's say

$array = ("BMW", "BMW", "BMW", "AUDI", "AUDI", "FORD", "FORD", "FORD");

So I want to count the values. I can do that using array_count_values ($array);

This creates another array containing BMW => 3, AUDI => 2, FORD => 3

So what I want to do now is move BMW & FORD into another array because they occurred three times and leave AUDI behind.

Any ideas?

Many thanks in advance!

4
  • 5
    Welcome! Stack Overflow is not a code writing service. We are always glad to help and support new coders but you need to help yourself first. You are expected to try to write the code yourself. Please read How to create a Minimal, Reproducible Example and How do I ask a good question?. Commented Jan 3, 2021 at 10:13
  • You want only the brands that occurred most frequent? Commented Jan 3, 2021 at 10:13
  • 2
    Loop again on them and collect them in a different array? Commented Jan 3, 2021 at 10:15
  • this would give you a hint stackoverflow.com/questions/55646668/… Commented Jan 3, 2021 at 18:22

2 Answers 2

1

Since everyone is throwing around answers let me also not miss out.... In case you want only those brands that occur most frequent in $array you can simply use an array_filter to filter those that match the maximum number of occurrences. Finally you can extract those keys to obtain the brands through array_keys.

<?php

$array = array("BMW", "BMW", "BMW", "AUDI", "AUDI", "FORD", "FORD", "FORD");
$frequencies = array_count_values($array);
$max = max(array_values($frequencies));

// If you don't want to do this dynamically you can simply change the
// filter condition of course.
$filtered = array_filter($frequencies, function ($val) use ($max) {
    // Since you mentioned you work with hard coded values,
    // you could change it to
    // return $val >= 3;
    return $val === $max;
});

$filteredBrands = array_keys($filtered);
$allBrands = array_keys($frequencies);
// We can then exclude the filtered set
// from the entire set to separate them
// into two sets.
$difference = array_diff($allBrands, $filteredBrands);

print_r($filteredBrands);
echo '</br>';
print_r($difference);
echo '</br>';
Sign up to request clarification or add additional context in comments.

Comments

0

This code will help you
First add the the element that we want in a new array ($newArray)
Then use the array_diff and update the first array

<?php

$array = ["BMW", "BMW", "BMW", "AUDI", "AUDI", "FORD", "FORD", "FORD"];
$countArray = (array_count_values ($array));

function youCondition($countArray, $element) {
    return $countArray[$element] == 3;
}

$newArray = [];
for ($i=0; $i < count($array); $i++) { 
    // put any condition that you want here
    if (youCondition($countArray, $array[$i])) {
        $newArray[] = $array[$i];
    } 
}

// your new generated array
print_r($newArray);
$array = array_diff($array, $newArray);

// your updated array
print_r($array);


output

Array
(
    [0] => BMW
    [1] => BMW
    [2] => BMW
    [3] => FORD
    [4] => FORD
    [5] => FORD
)
Array
(
    [3] => AUDI
    [4] => AUDI
)

And you can change your condition ($countArray[$array[$i]] == 3) and make the different arrays

4 Comments

If you hard code the count of 3 this way you might as well hard code the arrays from the start.... Also OP didn't clearly specify what a general solution would look like so how can you answer on this.
I make it more dynamic now
Thank you so much! I've changed the == to > for my needs and it works perfectly. ;-)
You're welcome , if my answer helped you with your question, I would appreciate it if you choose it as the best answer as you wish.

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.