2012-02-07 95 views
0

我想對數組進行排序,以便我可以基於一個參數(它們的電子郵件地址)從多維數組中抽取一些用戶。最終我希望將原始數組分成兩個不同的數組:一個使用選定的電子郵件地址(下面標記爲$js)和一個不是(下面標記爲$not_js)。使用PHP從多維數組中查找/排序多個值

這裏是我的我的代碼:

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

目前印刷$not_js返回所有4個用戶和$js沒有打印任何東西所以很明顯這是不正確的。任何想法將不勝感激!

回答

1

我認爲你會以這種錯誤的方式去做。 array_filter就像是一個array_map調用,它將第二個參數作爲可調用對象,它將返回一個布爾值,以確定該值是否存在於已過濾的集合中。 sort_jerry_and_sue函數的參數將是$users數組中的每個用戶元素,它們是一個接一個。

通過返回false,如果用戶有一個預定義的電子郵件地址,你基本上將它們從結果數組中排除。

一旦您有一組布爾過濾集合,您可以通過執行array_diff_assoc來獲得不同的讚美。

<?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); 
?> 
+0

我喜歡你更好:) – bobsoap 2012-02-07 04:13:57

+0

真棒sberry!非常感謝! -tim – 2012-02-07 04:16:16

+0

sberry,還有一個問題:我如何將sort_jerry_and_sue()函數之外的$ j_and_s數組取出?我試圖把它作爲第二個參數與$用戶並列,但這不起作用,有什麼想法?謝謝,-tim – 2012-02-07 05:16:26

0

嘗試通過users陣列循環的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; 
}