The Bobster
The Bobster

Reputation: 573

PHP array count unique occurrences of value

I have the following array:

$booking_array = array(

    "2018-09-01"=>array("360","360","360"),
    "2018-09-02"=>array("360","360"),
    "2018-09-03"=>array("360","520"),
    "2018-09-04"=>array("360","360"),
    "2018-09-05"=>array("360","520","520"),

);

echo '<pre>',print_r($booking_array),'</pre>';

I want to get only the highest number of occurrences of a specific value in the whole array.

So for example the highest number of occurrences for "360" is "3", and for "520" it is "2".

I'm using array_count_values like so:

foreach($booking_array as $key => $val) {

    print_r(array_count_values($val));

}

Which outputs:

Array
(
    [360] => 3
)

Array
(
    [360] => 2
)

Array
(
    [360] => 1
    [520] => 1
)

Array
(
    [360] => 2
)

Array
(
    [360] => 1
    [520] => 2
)

But I don't understand how I can take the highest values only and transform this into:

Array
(
    [0] => 360
    [1] => 360
    [2] => 360
    [3] => 520
    [4] => 520
)

Upvotes: 1

Views: 155

Answers (4)

Elementary
Elementary

Reputation: 1453

First we create a little function to make the things easier:

 function mergeIfGreaterThan($x,$y){
    /*
        this function merge two arrays .Each existing value of $x is 
        replaced by its corresponding in $y if it is greater than it.If a value doesn't
        exist in $x  but exists in $y it is automatically added to $x
    */
    if(!is_array($x)||!is_array($y)) return false;
    foreach($x as $k=>$v){
        if(isset($y[$k])){
            if($y[$k]>$v)
                $x[$k]=$y[$k];
            unset($y[$k]);
        }

    }
    if(!empty($y))
    foreach($y as $k=>$v){
        $x[$k]=$v;
    }
    return $x;
}

given this array:

$booking_array = array(

    "2018-09-01"=>array("360","360","360"),
    "2018-09-02"=>array("360","360"),
    "2018-09-03"=>array("360","520"),
    "2018-09-04"=>array("360","360"),
    "2018-09-05"=>array("360","520","520"),

);

we handle it like this:

$x=array();//create an empty array
foreach($booking_array as $value){//grab the max number of occurences of each value 
    $x=mergeifgreaterthan($x,array_count_values($value)); 
}
$result=array();//create another empty array to contain the result
foreach($x as $k=>$v){//loop through the array of number of occurences
    for($i=$v;$i>0;$i--){//append the value to the result array the number of times each value occurs
        $result[]=$k;
    }
}
print_r($result);

output:

Array
(
    [0] => 360
    [1] => 360
    [2] => 360
    [3] => 520
    [4] => 520
)

Upvotes: 2

Andreas
Andreas

Reputation: 23958

I think this is the easiest way to find the values you want.

This uses array_count_values and sorts the array descending and grabs the first item key.

foreach($booking_array as $booking) {
    $array_count=array_count_values($booking);
    arsort($array_count); // sort it descending
    $result[] = key($array_count);  // get first items key
}

var_dump($result);

https://3v4l.org/WrkRf

Upvotes: 3

Arun R. Prajapati
Arun R. Prajapati

Reputation: 2802

Follow this Steps

Step 1 : Create one Multidimensional Array to store Values and There Hightest Number of Presence

Step 2 : Go for Forloop or Foreach to Travese the Array

Step 3 : Find max from the Array and update the Multidimensional Array

Step 4 : Use the Multidimensional Array to Print as you required

if you have 5 presence of 320 and 3 presence of 540 then just

    $a=array();
    for($i=0;$i>arraycount;$i++)
       {
          array_push($a,"value");
       }

    print_r($a);

Upvotes: 0

sarin
sarin

Reputation: 35

$booking_array = array(

    "2018-09-01"=>array("520","360","360","360"),
    "2018-09-02"=>array("360","360"),
    "2018-09-03"=>array("360","520"),
    "2018-09-04"=>array("360","360"),
    "2018-09-05"=>array("360","520","520"),

);

echo '<pre>',print_r($booking_array),'</pre>';

foreach($booking_array as $booking) {
    $array_count=array_count_values($booking);
    $value = max($array_count);
    $key = array_search($value, $array_count);
    echo $key.'--'.$value.'times'.'<br/>';

}    

Try this

Upvotes: -1

Related Questions