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

问题描述 投票:0回答:5

我有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 还比较陌生。 希望你们中的某个人能让我重回正轨。

php arrays sorting
5个回答
3
投票

这将在一行中完成您想要的操作:

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

print_r($result);

解释一下: 由于您想按数组中的值排序,而不是按键排序,因此我们使用

array_flip
来翻转数组和每个数组中的值。然后,我们使用
array_replace
将第二个数组中的值替换为第一个数组中的匹配值(保持当前顺序)。然后我们使用
array_flip
将键和值恢复到我们开始时的状态。


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()


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;

}

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);

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);
© www.soinside.com 2019 - 2024. All rights reserved.