3

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

4

5 回答 5

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

于 2013-03-28T03:56:08.000 回答
3

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

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

print_r($result);

解释一下:由于您想按数组中的值而不是键进行排序,因此我们使用array_flip翻转每个数组中的数组和值。然后,我们用array_replace第一个数组中的匹配值替换第二个数组中的值(保持当前顺序)。然后我们使用array_flip将键和值放回我们开始的方式。

于 2013-03-28T04:08:09.873 回答
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;

}
于 2013-03-28T03:42:56.177 回答
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);
于 2013-03-28T03:55:58.997 回答
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);
于 2013-03-28T03:57:48.610 回答