我想对数组进行排序,以便我可以基于一个参数(它们的电子邮件地址)从多维数组中抽取一些用户。最终我希望将原始数组分成两个不同的数组:一个使用选定的电子邮件地址(下面标记为$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