douchen4534 2016-08-15 02: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 12: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部