blinks Posted December 15, 2009 Share Posted December 15, 2009 I have 2 arrays - $array1 = Array ( [0] => A:163 [1] => A:164 [2] => A:50) $array2 = Array ( [0] => Array ( [pid] => A:163 [date] => 2009-11-18 13:19:44 ) [1] => Array ( [pid] => A:102 [date] => 2009-03-27 17:53:49 ) [2] => Array ( [pid] => A:50 [date] => 2009-03-27 11:22:40 ) ) and want $array3 to contain those elements of $array2 where the value of $array 1 = value of $array2['pid'] i.e. $array3 = Array ( [0] => Array ( [pid] => A:163 [date] => 2009-11-18 13:19:44 ) [1] => Array ( [pid] => A:50 [date] => 2009-03-27 11:22:40 )) How can I do this? Link to comment https://forums.phpfreaks.com/topic/185178-array-intersect-question/ Share on other sites More sharing options...
rajivgonsalves Posted December 15, 2009 Share Posted December 15, 2009 I don't think you will be able to use array_intersect although you can do this <?php $array1 = array('A:163','A:223'); $array2 = array(array('A:163','2009-11-18 13:19:44'),array('A:1622','2009-11-18 13:19:44')); $array3 = array_filter($array2, create_function('$a', "global $array1;return in_array($a['pid'],$array1) ? $a : null;")); print_r($array3); ?> Link to comment https://forums.phpfreaks.com/topic/185178-array-intersect-question/#findComment-977567 Share on other sites More sharing options...
blinks Posted December 15, 2009 Author Share Posted December 15, 2009 Thank you for your reply. Unfortunately, I'm getting a "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING" error in relation to the $array3= line. Can you help once more? Link to comment https://forums.phpfreaks.com/topic/185178-array-intersect-question/#findComment-977661 Share on other sites More sharing options...
rajivgonsalves Posted December 15, 2009 Share Posted December 15, 2009 sorry my fault should have been <?php $array1 = array('A:163','A:223'); $array2 = array(array('pid'=>'A:163','date'=>'2009-11-18 13:19:44'),array('pid'=>'A:1622','date'=>'2009-11-18 13:19:44')); $array3 = array_filter($array2, create_function('$a', 'global $array1;return in_array($a[\'pid\'],$array1) ? $a : null;')); print_r($array3); ?> Link to comment https://forums.phpfreaks.com/topic/185178-array-intersect-question/#findComment-977724 Share on other sites More sharing options...
blinks Posted December 15, 2009 Author Share Posted December 15, 2009 Thanks heaps, Rajiv. This does just the job! Link to comment https://forums.phpfreaks.com/topic/185178-array-intersect-question/#findComment-977959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.