PHP将第二个数组中的匹配数据放在第一个数组的顶部

时间:2013-02-16 08:48:55

标签: php arrays

我有一个这样的数组:

1-  
    Array ( [55] => latest 
            [2] => block2 
            [3] => featured 
            [56] => News 
            [4] => portfolio 
            [57] => archive  )

另一个是用用户选择构建的,它看起来像这样:ie。

2-   Array ( [0] => 56 
             [1] => 55 
             [2] => 4)

所以从数组1中选择了3个Id,我要做的是让数组(2)上的那些值在第一个数组中重新排序,这样数组就像这样;

3- /*results*/ 

    Array ( [56] => News 
            [55] => latest 
            [4] => portfolio 
            [2] => block2 
            [3] => featured  
            [57] => archive  )

如果您发现我应该更改数组结构中的任何内容以获取或解决方案,请告诉我。

1 个答案:

答案 0 :(得分:0)

试试这个

arr1 =

Array ( [55] => latest 
        [2] => block2 
        [3] => featured 
        [56] => News 
        [4] => portfolio 
        [57] => archive  )

arr2 =

Array ( [0] => 56 
         [1] => 55 
         [2] => 4)

foreach($arr1 as $key1 => $val1)
{
    foreach($arr2 as $key2 => $val2)
    {
        if($val2 == $key1)
            $arr3[$val2] = $val1;
    }
}
print_r($arr3);

它会给你