PHP - compare and filter two arrays with different dimensions

北战南征 提交于 2019-12-23 04:21:19

问题


I am trying to filter two arrays to get a final result with user ids from my mysql database

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. This my "reference Array" that means i only need the ids (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 ($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...


回答1:


Try this (Working example : 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);

?>



回答2:


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

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



回答3:


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;
   }
}



回答4:


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'));



回答5:


better you use mysql itself do this task. 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
  }   
}


来源:https://stackoverflow.com/questions/18010717/php-compare-and-filter-two-arrays-with-different-dimensions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!