douxia2053 2017-09-22 15:33
浏览 68
已采纳

从具有键作为另一个数组的值的数组值中提取

Better show than tell.

$first = array(
    3=>"Banana", 
    4=>"Apple", 
    6=>"Lemon",
    7=>"Pineapple",
    8=>"Peach"
);

$second = array(4,7,8);

(Please note: the first one is associative array, it can have holes) The result should be

$result = array(
    "Apple", 
    "Pineapple",
    "Peach"
);

Any smart idea? Thank you

  • 写回答

3条回答 默认 最新

  • duanba4254 2017-09-22 15:39
    关注

    Here we are using array_intersect_key, array_flip and array_values. This single liner will be enough.

    1. array_values will return values of an array.

    2. array_flip will flip array over keys and values.

    3. array_intersect_key will return array on the basis of two input array's over intersecting keys.

    Try this code snippet here

    print_r(
         array_values(
             array_intersect_key(
                      $first, array_flip($second))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?