0

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']));
}
1
  • this might help Commented May 20, 2021 at 7:34

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);
        }

Comments

1
<?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
)

Comments

0

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);

1 Comment

Your output array only has entries for values which appeared (4, 5). It doesn't have entries for other values.
0

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

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.