按另一个数组键对数组值进行排序

时间:2013-03-28 03:31:11

标签: php arrays sorting

我有2个阵列:

Array
(
    [field_bathrooms] => Bathrooms
    [field_bedrooms] => Bedrooms
    [field_king_beds] => King Beds
    [field_kitchen] => Kitchen
    [field_queen_beds] => Queen Beds
    [field_sleeps_max] => Sleeps
    [field_sofa_beds] => Sofa Beds
    [field_sqft] => Square Footage
    [field_twin_beds] => Twin Beds
)

Array
(
    [0] => Bathrooms
    [1] => Square Footage
    [2] => King Beds
    [3] => Sofa Beds
    [4] => Sleeps
    [5] => Twin Beds
    [6] => Queen Beds
    [7] => Kitchen
    [8] => Bedrooms
)

我想按第二个数组的键对第一个数组进行排序,所以最终的结果是这样的数组:

Array(
[field_bathrooms] => Bathrooms
[field_sqft] => Square Footage
[field_king_beds] => King Beds
[field_sofa_beds] => Sofa Beds
[field_sleeps_max] => Sleeps
[field_twin_beds] => Twin Beds
[field_queen_beds] => Queen Beds
[field_kitchen] => Kitchen
[field_bedrooms] => Bedrooms
)

我必须承认我对PHP和MySQL相对较新。 希望你们中的一些人能够让我重回正轨。

5 个答案:

答案 0 :(得分:3)

您可以编写引用地图的自定义排序函数(反转的第二个数组):

$map = array_flip($second_array);

uasort($first_array, function($a, $b) use ($map) {
    return $map[$a] - $map[$b];
});

print_r($first_array);

另请参阅:array_flip() uasort()

答案 1 :(得分:3)

这将在一行中完成您想要的任务:

$result = array_flip( array_replace( array_flip($arr2), array_flip($arr1) ) );

print_r($result);

解释: 由于您希望按数组中的值排序,而不是按键排序,因此我们使用array_flip来翻转每个数组中的数组和值。然后,我们使用array_replace将第二个数组中的值替换为第一个数组中的匹配值(保持当前顺序)。然后我们使用array_flip将键和值放回原处。

答案 2 :(得分:0)

您的代码可能是这样的:

$indx;
for ($i = 0; i < Aarray1size; i++)
{

    $key = Array1[i];
    for ($j = 0; j < array2size; j++)
    {
         if(Array2[j] == $key)
          {
                index = j;
          }
    }
    Array1[i] = Array2[index];
    index = 0;

}

答案 3 :(得分:0)

要做到这一点

<?

$arr1 = array(
    'field_bathrooms' => 'Bathrooms',
    'field_bedrooms' => 'Bedrooms',
    'field_king_beds' => 'King Beds',
    'field_kitchen' => 'Kitchen',
    'field_queen_beds' => 'Queen Beds',
    'field_sleeps_max' => 'Sleeps',
    'field_sofa_beds' => 'Sofa Beds',
    'field_sqft' => 'Square Footage',
    'field_twin_beds' => 'Twin Beds'
);

$arr2 = array(
    'Bathrooms',
    'Square Footage',
    'King Beds',
    'Sofa Beds',
    'Sleeps',
    'Twin Beds',
    'Queen Beds',
    'Kitchen',
    'Bedrooms',
);

$result = array();
foreach($arr2 as $val)
{
    foreach($arr1 as $k => $v)
    {
        if ($val == $v)
        {
            $result[$k] = $v;
            unset($arr1[$k]);
            break;
        }
    }
}
var_dump($result);

答案 4 :(得分:0)

$array1 = array
(
    'field_bathrooms' => 'Bathrooms',
    'field_bedrooms' => 'Bedrooms',
    'field_king_beds' => 'King Beds',
    'field_kitchen' => 'Kitchen',
    'field_queen_beds' => 'Queen Beds',
    'field_sleeps_max' => 'Sleeps',
    'field_sofa_beds' => 'Sofa Beds',
    'field_sqft' => 'Square Footage',
    'field_twin_beds' => 'Twin Beds',
);

$array2 = array
(
    0 => 'Bathrooms',
    1 => 'Square Footage',
    2 => 'King Beds',
    3 => 'Sofa Beds',
    4 => 'Sleeps',
    5 => 'Twin Beds',
    6 => 'Queen Beds',
    7 => 'Kitchen',
    8 => 'Bedrooms',
);

$array3 = array();
foreach ($array2 as $val)
{
  $key = array_search($val, $array1);
  $array3[$key] = $val;
}

var_dump($array3);