我想對數組進行排序,以便我可以基於一個參數(它們的電子郵件地址)從多維數組中抽取一些用戶。最終我希望將原始數組分成兩個不同的數組:一個使用選定的電子郵件地址(下面標記爲$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
沒有打印任何東西所以很明顯這是不正確的。任何想法將不勝感激!
我喜歡你更好:) – bobsoap 2012-02-07 04:13:57
真棒sberry!非常感謝! -tim – 2012-02-07 04:16:16
sberry,還有一個問題:我如何將sort_jerry_and_sue()函數之外的$ j_and_s數組取出?我試圖把它作爲第二個參數與$用戶並列,但這不起作用,有什麼想法?謝謝,-tim – 2012-02-07 05:16:26