简体   繁体   English

PHP-比较和过滤具有不同维数的两个数组

[英]PHP - compare and filter two arrays with different dimensions

I am trying to filter two arrays to get a final result with user ids from my mysql database 我正在尝试过滤两个数组以从MySQL数据库中获得带有用户ID的最终结果

I have two arrays the first one: 我有两个数组第一个:

print_r($arr_partner_id);
Array ( 
[0] => Array ( [id] => 335 [id_partner] => 0 ) 
[1] => Array ( [id] => 469 [id_partner] => 1 ) 
[2] => Array ( [id] => 457 [id_partner] => 1 ) 
[3] => Array ( [id] => 339 [id_partner] => 0 ) 
[4] => Array ( [id] => 361 [id_partner] => 0 ) ) 

and the second one: 第二个:

print_r($arr_member_id);
Array ( 
[0] => 457 
[1] => 469 
[2] => 339 
[3] => 361 ) 

now i want compare these two only with their ids and delete the ids that are not included in the "$arr_member_id" Array. 现在,我只想将这两个ID与它们的ID进行比较,并删除“ $ arr_member_id”数组中未包含的ID。 This my "reference Array" that means i only need the ids (457,469,339,361) 这是我的“引用数组”,这意味着我只需要ID(457,469,339,361)

for the final result it should be looking like this: 对于最终结果,它应该看起来像这样:

print_r($arr_partner_final_id);
Array ( 
[0] => Array ( [id] => 469 [id_partner] => 1 ) 
[1] => Array ( [id] => 457 [id_partner] => 1 ) 
[2] => Array ( [id] => 339 [id_partner] => 0 ) 
[3] => Array ( [id] => 361 [id_partner] => 0 ) ) 

i tryed it with foreach 我尝试过foreach

foreach ($arr_partner_id as $key => $usr_ids) {
    if($arr_partner_id[$key]['id'] ==  $arr_member_id[$key]) {
        // do something
    }   
}

but the "keys" are different this should not working... 但“键”不同,这不起作用...

making it as simple, and using just one loop to loop through the array and checkin if the id is present in another set of array using in_array() 使其变得简单,只需使用一个循环即可遍历数组,并使用in_array()检查ID是否存在于另一组数组中

try this 尝试这个

for($i=0;$i<count($arr_partner_id);$i++){
  if(!in_array($arr_partner_id[$i]['id'],$arr_member_id)){
     unset($arr_partner_id[$i]);
  }
}

print_r($arr_partner_id);

try it here 在这里尝试

AND yes!! 是的!! if you want seperate arrays for that then simply modify the code..create new array and push the elements that is present in array 如果您要使用单独的数组,则只需修改代码即可。创建新数组并推送数组中存在的元素

$finalArray=array();
for($i=0;$i<count($arr_partner_id);$i++){
  if(in_array($arr_partner_id[$i]['id'],$arr_member_id)){
    $finalArray[]=$arr_partner_id[$i];
 }
}

print_r($finalArray);

Try this (Working example : http://codepad.org/ApFcA3Zo ) 试试这个(工作示例: http : //codepad.org/ApFcA3Zo

<?php

$arr_partner_id=array ( 
'0' => array ( 'id' => 335, 'id_partner' => 0 ) ,
'1' => array ( 'id' => 469, 'id_partner' => 1 ) ,
'2' => array ( 'id' => 457, 'id_partner' => 1 ) ,
'3' => array ( 'id' => 339, 'id_partner' => 0 ) ,
'4' => array ( 'id' => 361, 'id_partner' => 0 ) ) ;


$arr_member_id=array ( 
'0' => 457 ,
'1' => 469 ,
'2' => 339 ,
'3' => 361 ) ;

$final =array();

foreach($arr_partner_id as $arr)
{
  foreach($arr_member_id as $parr)
  {
     if($arr['id'] == $parr)
    {
    $final[]=$arr;
    }
   } 
}


print_r($final);

?>

Maybe something like: 也许像:

foreach ($arr_member_id as $usr_id){
   foreach ($arr_partner_id as $partner){
     if ($usr_id == $partner['id']) {$arr_partner_final_id[]=$partner;break;
   }
}

Another solution (without explicit looping): 另一个解决方案(无显式循环):

$arr_partner_id=array ( 
'0' => array( 'id' => 335, 'id_partner' => 0 ),
'1' => array( 'id' => 469, 'id_partner' => 1 ),
'2' => array( 'id' => 457, 'id_partner' => 1 ),
'3' => array( 'id' => 339, 'id_partner' => 0 ),
'4' => array( 'id' => 361, 'id_partner' => 0 ));


$arr_member_id=array ( 
'0' => 457,
'1' => 469,
'2' => 339,
'3' => 361);


function compare($v){global $arr_member_id;return in_array($v['id'], $arr_member_id);}
var_dump($arr_partner_id = array_filter($arr_partner_id, 'compare'));

better you use mysql itself do this task. 最好您使用mysql本身来完成此任务。 but if you need to continue with this use in array function to check the second array as given below, 但是,如果您需要继续在数组函数中使用此功能,以检查第二个数组,如下所示,

foreach ($arr_partner_id as $key => $usr_ids) {
  if(in_array($usr_ids["id"], $arr_member_id)) {
    // do something
  }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM