Jump to content

Array intersect question


blinks

Recommended Posts

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

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


?> 

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


?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.