-1

What exactly wrong in this code?

      $user_friends_bday_set = array();
      function isFullDate($target, $array) {
        array_filter($array, function($friendo){
          if(isset($friendo['bdate']) && strlen($friendo['bdate'])>5 ){
            array_push($target, $friendo);
            return $target;
          }//IF
        });//ARRAYFILTER
      };

  $user_friends_bday_set = isFullDate($user_friends_bday_set, $user_friends_arr);

Why $user_friends_bday_set is null?

1 Answers1

3

$target is unknown to your function($friendo) .... Change your code to

$user_friends_bday_set = array();
function isFullDate($target, $array) {
  array_filter($array, function($friendo) use (&$target) {
    if(isset($friendo['bdate']) && strlen($friendo['bdate'])>5 ){
      array_push($target, $friendo);
        return $target;
    }//IF
  });//ARRAYFILTER
};

use(&$target) has been added so your closure knows about $target.

Read more: https://www.php.net/manual/en/functions.anonymous.php

brombeer
  • 8,716
  • 5
  • 21
  • 27