将数组索引更改为自定义顺序php

时间:2015-03-02 10:33:23

标签: php arrays

我有一个索引数组如下

$item = array("one","two","three","four");

我需要用另一个数组

更改这个数组的索引
$indexarray = array("2","0","3","1");

将新的intex分配给$ item数组,如下所示,

one ---> 2
two ---> 0
three ---> 3
four ---> 1

所以需要的结果数组应该是这样的

$item = array("two","four","one","three"); 

我怎样才能实现这一点,请帮助

4 个答案:

答案 0 :(得分:3)

Use:

$c=array_combine($indexarray ,$item );
print_r($c);

答案 1 :(得分:1)

使用此

$indexarray = array("2","0","3","1");
$item = array("one","two","three","four");
$result=array_combine($indexarray ,$item );
print_r($result);

答案 2 :(得分:0)

<?php    
    $item = array("one","two","three","four");
    $indexarray = array("2","0","3","1");
    $item_new=array();
    foreach($indexarray as $key=>$value)
    {
        $item_new[$key]=$item[$value];
    }
    $item=$item_new;
    print_r($item);
?>

答案 3 :(得分:0)

首先合并两个数组

$item = array("one","two","three","four");
$indexArray = array("2","0","3","1");

$tempArray = array_combine($indexArray, $item);

然后对数组进行排序

ksort($tempArray);