2

我有一个看起来像这样的数组,按城市名称排序。PHP中最简单的方法是对该数组进行排序以在顶部显示纽约,然后是伦敦,然后其余部分将按照现在的城市名称进行排序。请记住,这是一个很长的数组,我为这个问题缩短了。

Array
(
    [0] => Array
        (
            [0] => Berlin
            [1] => 1
        )

    [1] => Array
        (
            [0] => London
            [1] => 2
        )

    [2] => Array
        (
            [0] => New York
            [1] => 4
        )

    [3] => Array
        (
            [0] => Paris
            [1] => 5
        )

)
4

4 回答 4

0

希望这可以帮助

$arrayCity; //your array
$newYork = null;
$london = null;
// Get London and New York and remove them
foreach ($arrayCity as $key => $item) {
    if ($item[0] == "New York") {
        $newYork = $item;
        unset($arrayCity[$key]);
    } elseif ($item[0] == "London") {
        $london = $item;
        unset($arrayCity[$key]);
    }
    // break if they were found
    if ($newYork && $london) {
        break;
    }
}
// Add them at the top
array_unshift($arrayCity, $newYork, $london);
于 2012-07-05T09:37:14.810 回答
0

一种贫民区解决方案:

$arrayCityIndex = array(); // Creating an array for sorting the keys while preserving index numbers.
$arrayNYLondonCityIndex = array();

foreach ($originalArray as $index => $cityArray) // Iterating through the original array.
{
    if ($cityArray[0] != 'New York' && $cityArray[0] != 'London') // Adding non NY and non London to array of indexes first.
    {
        $arrayCityIndex[$cityArray[0]] = $index; // Creates a City Name => Index Number hash.
    }
    else if ($cityArray[0] == 'New York' || $cityArray[0] = 'London')
    {
        $arrayNYLondonCityIndex[$cityArray[0]] = $index;
    }
}

ksort($arrayCityIndex); // Sort the index array by the key

$sortedArray = array();

foreach ($arrayNYLondonCityIndex as $cityName => $index) // Iterate through the hash of indexes and for each city in the hash, return the city's array in the original array (using the associated index number), and add it to the sorted array.
{
    $sortedArray[] = $originalArray[$index];
}

foreach ($arrayCityIndex as $cityName => $index)
{
    $sortedArray[] = $originalArray[$index];
}

var_dump($sortedArray);

这是处理解决方案的一种相当老套的方法,但我认为它应该可以工作。

祝你好运!

于 2012-07-05T09:37:30.067 回答
0

使用二分搜索逻辑搜索 London 和 NewYork,删除并将其放在前面。

$cityList=<the above array>;
$key="London";
$start=0;
$end=count($cityList);
$mid=($start+$end)/2;
while($end>=$start)
{
if($cityList[$mid][0]==$key)
{
    $keyForLondon=$mid;
    break;
}
elseif(strcmp($cityList[$mid][0],$key)<0)
    $start=$mid+1;
else
    $end=$mid-1;
$mid=($start+$end)/2;
}
$temp=$cityList[$mid];
unset($cityList[$mid]);
array_unshift($temp);
<Similar for New York>
$cityList=array_values(array_filter($cityList));
于 2012-07-05T09:38:43.687 回答
0

您可以将内置函数usort()与自定义排序函数一起使用:

    // Input array
    $cities = array(
            array('Berlin', 1),
            array('London', 2),
            array('New York', 4),
            array('Paris', 5),
            array('Test', 12),
            array('Zzz', 3)
    );

    // Custom sort function
    function customFilter($a, $b)
    {
            static $firstEntries = array('New York', 'London');

            foreach ($firstEntries as $name)
            {
                    if ($a[0] == $name)
                            return -1;
                    if ($b[0] == $name)
                            return 1;
            }

            // Unprioritized : sort by name
            return strcmp($a[0], $b[0]);
    }

// Original array order
var_dump($cities);

// Filter and dump of the new array order
usort($cities, 'customFilter');
var_dump($cities);

$firstEntries数组是可定制的,并将优先考虑定义的名称。

于 2012-07-05T09:58:38.000 回答