Search code examples
phparraysmultidimensional-arrayarray-filterarray-push

find/sort multiple values from multidimensional array using PHP


I'd like to sort an array such that i can extract some users from a multidimensional array based on one parameter, their email addresses. Ultimately I'd like to have the original array separated as 2 different arrays: one with chose email address (labeled $js below) and one's not (labeled $not_js below).

Here's my my code:

<?php
$users['1'] = array('name'=>'bob barker',
               'email'=>'bb@bb.com',
                  'id'=>'1');

$users['2'] = array('name'=>'jerry jones',
               'email'=> 'jj@jj.com',
                  'id'=>'2' );

$users['3'] = array('name'=>'sue smith',
               'email'=>'ss@ss.com',
                  'id'=>'3' );

$users['4'] = array('name'=>'zach zed',
               'email'=>'zz@zz.com',
                  'id'=>'4' );

function sort_jerry_and_sue($users)
{
    $j_and_s=array('jj@jj.com', 'ss@ss.com');
    $not_js=array();
    foreach($j_and_s as $row=>$js){
            if(in_array($js, $users)){
            }
            else {
                array_push($not_js, $users);
            }
    }
    return $not_js;
}

$not_js = array_filter($users, 'sort_jerry_and_sue');

print_r($not_js);
// this is what i'd like it to print=> $not_js = Array ([0] => Array ( [name] => bob barker [email] => bb@bb.com [id] => 1 )    [1] => Array ( [name] => zach zed [email] => zz@zz.com [id] => 4 ))
print_r($js);
// this is what i'd like it to print=> $js = Array ([0] => Array ( [name] => jerry jones [email] => jj@jj.com [id] => 2 ) [1]   => Array ( [name] => sue smith [email] => ss@ss.com [id] => 3 ))
?>

Currently printing $not_js returns all 4 users and the $js doesn't print anything so obviously that's not right. Any thoughts would be greatly appreciated!


Solution

  • I think you are going about this the wrong way. array_filter is like a array_map call that takes as its second argument a callable that will return a boolean determining whether or not the value exists in the filtered set. The argument to the sort_jerry_and_sue function will be each user element of the $users array, one by one.

    By returning false if the user has one of the predefined email addresses, you essentially filer them out of the resultant array.

    Once you have one set of a boolean filtered set, you get the compliment by doing an array_diff_assoc to get the difference.

    <?php
    
    
    class EmailFilter {
    
      private $emails = array();
    
      public function __construct(array $emails) {
        $this->emails = $emails;
      }
    
      public function filter_users($user) {
        return in_array($user['email'], $this->emails);
      }
    
    }
    
    $users['1'] = array('name'=>'bob barker', 'email'=>'bb@bb.com', 'id'=>'1');
    $users['2'] = array('name'=>'jerry jones', 'email'=> 'jj@jj.com', 'id'=>'2' );
    $users['3'] = array('name'=>'sue smith', 'email'=>'ss@ss.com', 'id'=>'3' );
    $users['4'] = array('name'=>'zach zed', 'email'=>'zz@zz.com', 'id'=>'4' );
    
    $filter_emails = array('jj@jj.com', 'ss@ss.com');
    $not_js = array_filter($users, array(new EmailFilter($filter_emails), 'filter_users'));
    $js = array_diff_assoc($users, $not_js);
    
    // this is what i'd like it to print=> $not_js = Array ([0] => Array ( [name] => bob barker [email] => bb@bb.com [id] => 1 )    [1] => Array ( [name] => zach zed [email] => zz@zz.com [id] => 4 ))
    print_r($not_js);
    // this is what i'd like it to print=> $js = Array ([0] => Array ( [name] => jerry jones [email] => jj@jj.com [id] => 2 ) [1]   => Array ( [name] => sue smith [email] => ss@ss.com [id] => 3 ))
    print_r($js);
    ?>