twan
twan

Reputation: 2659

How to check how many times a number in an array appears in another PHP array

Say for example I have an array that looks like this:

$array = array(1, 2, 3, 4, 5);

And I have another array ($getreviewsarr) that contains this:

Array
(
    [0] => Array
        (
            [beoordeling] => 4
        )

    [1] => Array
        (
            [beoordeling] => 4
        )

    [2] => Array
        (
            [beoordeling] => 5
        )

    [3] => Array
        (
            [beoordeling] => 4
        )

    [4] => Array
        (
            [beoordeling] => 5
        )

    [5] => Array
        (
            [beoordeling] => 5
        )

)

How can I check how many times the 1 occurs and the 2 and the 3 etc.

So above example desired output would be:

1 - 0
2 - 0
3 - 0
4 - 3
5 - 3

I tried looping $getreviewsarr and using array_count_values on beoordeling but this doesn't show anything.

foreach($getreviewsarr as $occurence){
    print_r(array_count_values($getreviewsarr['beoordeling']));
}

Upvotes: 0

Views: 75

Answers (5)

Andreas
Andreas

Reputation: 23958

If you use array_fill and array_combine to transform $array to a key value pair with zero as value, then you can use array replace to "merge" it with the result of array_count_values.

$array = array(1, 2, 3, 4, 5);

$getreviewsarr = [
    ['beoordeling' => 4],
    ['beoordeling' => 4],
    ['beoordeling' => 4],
    ['beoordeling' => 5],
    ['beoordeling' => 5],
    ['beoordeling' => 5],
];
$array = array_combine($array, array_fill(0,count($array),0));
// Or as Nigel (https://stackoverflow.com/users/1213708) suggested:
// $array = array_fill_keys($array, 0);
// $array = [1=>0, 2=>0, 3=>0, 4=>0, 5=> 0]
$result = array_replace($array, array_count_values(array_column($getreviewsarr, 'beoordeling')));

print_r($result);

Output:

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

https://3v4l.org/gVvAd

Upvotes: 3

XMehdi01
XMehdi01

Reputation: 1

Create $views array contains only views. Then Create another array $occurences which count the number of occurence of each view.

$views = array_map(fn($view)=>$view['beoordeling'], $getreviewsarr);
$occurences = array_count_values($views);

Loop through your $array and display its value with the corresponding number of occurence, if there is no occurence use null coalscing operator ??.

foreach ($array as $value) {
    echo $value." - ".($occurences[$value]??0)."<br>";
}

Outputs:

1 - 0
2 - 0
3 - 0
4 - 3
5 - 3

Upvotes: 0

Definitely not Rafal
Definitely not Rafal

Reputation: 308

<?php
function custom($needleArray, $haystackArray) {
    // Prepare result array
    $result = [];
    foreach ($needleArray as $needle) {
        $result[$needle] = 0;
    }
    
    // Loop through the whole array ...
    foreach ($haystackArray as $tmpArray) {
        
        // .. and check if it should be counted in the result array ...
        $haystack = $tmpArray['beoordeling'];
        if (array_key_exists($haystack, $result)) {
            
            // .. if so increase the count by 1
            $result[$haystack]++;
        }
    }
    
    return $result;
}

// Some sample data
$testArray = [
    ['beoordeling' => 4],
    ['beoordeling' => 4],
    ['beoordeling' => 5],
    ['beoordeling' => 4],
    ['beoordeling' => 5],
    ['beoordeling' => 5],
];
$array = array(1, 2, 3, 4, 5);

// Print result ...
print_r(custom($array, $testArray));

Gives you

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

Upvotes: 1

Peter
Peter

Reputation: 62

Try this


function searchForId($id, $array) {
      $keys = array();
      foreach ($array as $key => $val) {
          if ($val['beoordeling'] === $id) {
              array_push($keys, $key);
              
          }
      }
      return $keys;
      //return null;
  }

Then use the function like this:

foreach($getreviewsarr as $key => $value){
    
          $ids = searchForId($value['beoordeling'], $array );
           print_r($ids); // the index of arrays where matches were found
          $newArr = array();
          foreach ($ids as $id){
          
              array_push($newArr, $array [$id]);  
              //  array of indexes where matches were found
          }
           echo "<br><br><br>";
           print_r($newArr);
        }

Upvotes: 1

User863
User863

Reputation: 20039

Try using array_column()

$getreviewsarr = [
    ['beoordeling' => 4],
    ['beoordeling' => 4],
    ['beoordeling' => 4],
    ['beoordeling' => 5],
    ['beoordeling' => 5],
    ['beoordeling' => 5],
];

$result = array_count_values(array_column($getreviewsarr, 'beoordeling'));

print_r($result);

Upvotes: 0

Related Questions