首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

回到两个数组的交集

2012-11-14 
返回两个数组的交集A数组PHP codearray([0] array([action_id] 3)[1] array([action_id] 2)[2

返回两个数组的交集
A数组

PHP code
array(    [0] =>        array(        ['action_id'] =>        3        )    [1] =>        array(        ['action_id'] =>        2    )    [2] =>        array(        ['action_id'] =>        1    )    [3] =>        array(        ['action_id'] =>        7    )    [4] =>        array(        ['action_id'] =>        11    ))

B数组:
PHP code
array(    [0] =>    array(    ['action_id'] =>    3        ['type'] =>        0        ['order_num'] =>        67    )    [1] =>    array(    ['action_id'] =>    2        ['type'] =>        0        ['order_num'] =>        66    )    [2] =>    array(    ['action_id'] =>    1        ['type'] =>        0        ['order_num'] =>        65    )    [3] =>    array(    ['action_id'] =>    7        ['type'] =>        0        ['order_num'] =>        64        )    [8] =>    array(    ['action_id'] =>    14        ['type'] =>        0        ['order_num'] =>        40        )    [13] =>    array(    ['action_id'] =>    11        ['type'] =>        0        ['order_num'] =>        30    ))

使用array_intersect_key返回的为什么是
PHP code
array(    [0] =>    array(    ['action_id'] =>    3        ['type'] =>        0        ['order_num'] =>        67    )    [1] =>    array(    ['action_id'] =>    2        ['type'] =>        0        ['order_num'] =>        66    )    [2] =>    array(    ['action_id'] =>    1        ['type'] =>        0        ['order_num'] =>        65    )    [3] =>    array(    ['action_id'] =>    7        ['type'] =>        0        ['order_num'] =>        64        )    )

action_id为11的为什么没了??

[解决办法]
PHP code
$a = array(  0 => array('action_id' => 3),  1 => array('action_id' => 2),  2 => array('action_id' => 1),  3 => array('action_id' => 7),  4 => array('action_id' => 11),);$b = array(  0 => array('action_id' => 3, 'type' => 0, 'order_num' => 67),  1 => array('action_id' => 2, 'type' => 0, 'order_num' => 66),  2 => array('action_id' => 1, 'type' => 0, 'order_num' => 65),  3 => array('action_id' => 7, 'type' => 0, 'order_num' => 64),  8 => array('action_id' => 14, 'type' => 0, 'order_num' => 40),  13 => array('action_id' => 11, 'type' => 0, 'order_num' => 30),);foreach($a as $v) $dict[] = $v['action_id'];foreach($b as $k=>$v) if(in_array($v['action_id'], $dict)) $c[$k] = $v;print_r($c); 

热点排行