0

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'=>'[email protected]',
                  'id'=>'1');

$users['2'] = array('name'=>'jerry jones',
               'email'=> '[email protected]',
                  'id'=>'2' );

$users['3'] = array('name'=>'sue smith',
               'email'=>'[email protected]',
                  'id'=>'3' );

$users['4'] = array('name'=>'zach zed',
               'email'=>'[email protected]',
                  'id'=>'4' );

function sort_jerry_and_sue($users)
{
    $j_and_s=array('[email protected]', '[email protected]');
    $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] => [email protected] [id] => 1 )    [1] => Array ( [name] => zach zed [email] => [email protected] [id] => 4 ))
print_r($js);
// this is what i'd like it to print=> $js = Array ([0] => Array ( [name] => jerry jones [email] => [email protected] [id] => 2 ) [1]   => Array ( [name] => sue smith [email] => [email protected] [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!

2 Answers 2

1

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'=>'[email protected]', 'id'=>'1');
$users['2'] = array('name'=>'jerry jones', 'email'=> '[email protected]', 'id'=>'2' );
$users['3'] = array('name'=>'sue smith', 'email'=>'[email protected]', 'id'=>'3' );
$users['4'] = array('name'=>'zach zed', 'email'=>'[email protected]', 'id'=>'4' );

$filter_emails = array('[email protected]', '[email protected]');
$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] => [email protected] [id] => 1 )    [1] => Array ( [name] => zach zed [email] => [email protected] [id] => 4 ))
print_r($not_js);
// this is what i'd like it to print=> $js = Array ([0] => Array ( [name] => jerry jones [email] => [email protected] [id] => 2 ) [1]   => Array ( [name] => sue smith [email] => [email protected] [id] => 3 ))
print_r($js);
?>
1
  • sberry, one more question: how would i take the $j_and_s array outside of the sort_jerry_and_sue() function? I've tried to make it a 2nd parameter alongside $users but that doesn't work, any thoughts? thanks, -tim Commented Feb 7, 2012 at 5:16
0

Try looping through the users array instead of j_and_s:

function sort_jerry_and_sue($users)
{
    $j_and_s=array('[email protected]', '[email protected]');
    $not_js=array();
    foreach($users as $js){
            if(in_array($js['email'], $j_and_s)){
            }
            else {
                array_push($not_js, $users);
            }
    }
    return $not_js;
}

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.