Wednesday, 15 February 2012

php - I want the value from my array if it does not appear in another array -



php - I want the value from my array if it does not appear in another array -

how can desired output? want check if array1 "entry_id" not same array2 "entry_id"

how can that? tried array_diff_key .. did not work

array1

array ( [0] => array ( [entry_id] => 53242 [site_id] => 2 [channel_id] => 2 [author_id] => 5 [1] => array ( [entry_id] => 76435 [site_id] => 8 [channel_id] => 8 [author_id] => 9

array2

array ( [0] => array ( [entry_id] => 89764 [site_id] => 1 [channel_id] => 1 [author_id] => 5 [1] => array ( [entry_id] => 53242[site_id] => 7 [channel_id] => 9 [author_id] => 1

i compare array1 , array2 each other , desired output:

desired output:

array ( [0] => array ( [entry_id] => 76434 [site_id] => 8 [channel_id] => 8 [author_id] => 9 [1] => array ( [entry_id] => 76435 [site_id] => 8 [channel_id] => 8 [author_id] => 9

for smallish size of input arrays can long way using 2 loops.

sample code (not tested!):

// $array1 , $array2 inputs // $array3 output $array3=array(); // maintain track of have found $buff=array(); foreach($array1 $a1){ foreach($array2 $a2){ // check matching ids , not used if($a1['entry_id']==$a2['entry_id'] , !isset($buff[$a1['entry_id']])){ $array3[]=$a1; $buff[$a1['entry_id']]=1; } } } print_r($array3); // debug

edit: op wanted opposite, so...

sample code (tested!):

// 2 pass filter // $array1 , $array2 inputs $array1=array( array('entry_id'=>1,'t'=>"a1"), array('entry_id'=>2,'t'=>"b1"), array('entry_id'=>3,'t'=>"c1"), ); $array2=array( array('entry_id'=>1,'t'=>"a2"), array('entry_id'=>2,'t'=>"b2"), array('entry_id'=>4,'t'=>"d2"), ); // $array3 output $array3=array(); // maintain track of have found $buff=array(); foreach($array1 $a1) $buff[$a1['entry_id']]=array(1,$a1); foreach($array2 $a2){ if(isset($buff[$a2['entry_id']])){ $buff[$a2['entry_id']]=array(2,$a2); }else{ $buff[$a2['entry_id']]=array(1,$a2); } } foreach($buff $k=>$v) if($v[0]==1) $array3[]=$v[1]; print_r($array3); // debug

php arrays

No comments:

Post a Comment