douchen4534 2016-08-15 10:56
浏览 30
已采纳

重新排列并获得PHP数组的等效值

Is there a way on PHP where I can get the arrangement of the strings inside $input array:

$input = array(3) {
    [0]=>
    string(3) "one"
    [1]=>
    string(3) "two"
    [2]=>
    string(5) "three"
}

Then use the $reference array as reference for the equivalent

$reference = array(3) {
    [0]=>
    array(2) {
        [0]=>
        string(1) "2"
        [1]=>
        string(3) "two"
    }
    [1]=>
    array(2) {
        [0]=>
        string(1) "3"
        [1]=>
        string(5) "three"
    }
    [2]=>
        array(2) {
        [0]=>
        string(1) "1"
        [1]=>
        string(3) "one"
    }
}

And result to the $output array?

$output = array(3) {
    [0]=>
    string(3) "1"
    [1]=>
    string(3) "2"
    [2]=>
    string(5) "3"
}
  • 写回答

3条回答 默认 最新

  • dongxian4531 2016-08-15 20:35
    关注

    You can reindex your reference array by the second column ("two", "three", etc.) using array_column.

    $words = array_column($reference, 0, 1);
    

    Then get your output by looking up the key corresponding to each value from $input in the reindexed array.

    $output = array_map(function($x) use ($words) {
        return $words[$x];
    }, $input);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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