0

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...

2
  • In your foreach you specify the key as $key1, but in the if statement you use $key, is this a typo in the question, or is your code like this? Commented Aug 2, 2013 at 7:08
  • sry a typing error from me...i correct it. Commented Aug 2, 2013 at 7:10

5 Answers 5

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

with that solution the index begins with "1" not with "0" because in this case the first row was affected...but if for example the third row is affected the index is (0,1,3,4) i am not sure but this can bring problems...
hmm.. the first solution has that problem.. since it deletes the index..and should use it only when you have nothing to do with array index.. anyways i provided the second solution.. with which it shouldn't matter... creating new array and pushing the elements that is present in the array.. :)
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 Comments

There should be a break inside the inner foreach to exit the loop when the condition is met, so you don't waste computer cycles unnecessarily.
@Borgtex Thanks for your comment. Will keep that in mind in future.
0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.