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 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错