doucitan2544 2019-07-19 11:48
浏览 82
已采纳

获取在php中的第二个数组中具有键的数组元素[duplicate]

This question already has an answer here:

I need only array elements which have their keys in values of second array, with the fastest script.

$array_1 = array(
    'ana' => 'are',
    'du'  => 34,
    'bopq'=> "Ana are mere mereu",
    13    => "OK"
);

$array_2 = array('du', 13);

$expected_array = array(
    'du' => 34,
    13   => "OK"
);

There is faster solution than:

$expected_array = array_intersect($array_1, array_combine($array_2, $array_2);

?

</div>
  • 写回答

1条回答 默认 最新

  • doufan9395 2019-07-19 11:54
    关注

    Array_intersect_key is what you are looking for.

    $array_1 = array(
        'ana' => 'are',
        'du'  => 34,
        'bopq'=> "Ana are mere mereu",
        13    => "OK"
    );
    
    $array_2 = array('du', 13);
    
    $result = array_intersect_key($array_1, array_flip($array_2));
    

    https://3v4l.org/P6jkq

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?