简体   繁体   English

通过继续值对Usort排序多维数组

[英]Usort sort multidimensional array by continuing values

I have an array: 我有一个数组:

$cards = [
    [
        "from" => "Barcelona",
        "to" => "Gerona Airport",        
    ],
    [
        "from" => "Stockholm",
        "to" => "New York JFK",          
    ],
    [
        "from" => "Gerona Airport",
        "to" => "Stockholm",          
    ],
    [
        "from" => "Madrid",
        "to" => "Barcelona",          
    ]
];

I need to sort it to make a continuing trip, where the from matches the previous to , or be set as the start if not, like: 我需要对它进行排序,使持续的旅程,其中from匹配前面to ,或者被设定为开始如果不是,如:

Array (
    [0] => Array ( [from] => Madrid [to] => Barcelona )  
    [1] => Array ( [from] => Barcelona [to] => Gerona Airport )
    [2] => Array ( [from] => Gerona Airport [to] => Stockholm ) 
    [3] => Array ( [from] => Stockholm [to] => New York JFK ) 
)

I am trying to use usort : 我正在尝试使用usort

usort($cards, function ($a, $b) {
          return ( $a["to"] === $b["from"] ) ? -1 : 1;
      });

print_r($cards);

But it's does not sort in the manner described. 但它并没有按照描述的方式排序。 Any ideas? 有任何想法吗?

Hopefully a better solution will come along, but if you sort as many times as there are elements in the array and return equal or greater then it works; 希望有一个更好的解决方案,但如果你排序的次数与数组中的元素一样多,并返回相等或更大,那么它的工作原理; even if you don't know the beginning or if the beginning is not the first element. 即使你不知道开头或开头不是第一个元素。 Because of the way usort traverses and compares, returning equal or less does not work: 由于usort遍历和比较的方式,返回相等或更少不起作用:

foreach($cards as $card) {
    usort($cards, function ($a, $b) {
                      return ( $a["to"] === $b["from"] ) ? 0 : 1;
                  });
}

It may work with fewer iterations, but comparing each against all others seems safe. 它可以使用较少的迭代,但是将每个迭代与所有其他迭代相比似乎是安全的。

You might take a recursive approach and specify the city to start from. 您可以采用递归方法并指定要从哪个城市开始。

By default, you start at Madrid with doSort($cards, $result); 默认情况下,你从马德里开始使用doSort($cards, $result); .

If you want to get the continuing trip from "Gerona Airport", you could start it with doSort($cards, $result, "Gerona Airport"); 如果你想从“赫罗纳机场”继续旅行,你可以用doSort($cards, $result, "Gerona Airport");

$cards = [
    [
        "from" => "Barcelona",
        "to" => "Gerona Airport",
    ],
    [
        "from" => "Stockholm",
        "to" => "New York JFK",
    ],
    [
        "from" => "Gerona Airport",
        "to" => "Stockholm",
    ],
    [
        "from" => "Madrid",
        "to" => "Barcelona",
    ]
];
$result = [];

function doSort($cards, &$result, $start = "Madrid") {
    foreach ($cards as $key => $card) {
        if ($card["from"] === $start) {
            $result[] = $card;
            doSort($cards, $result, $card["to"]);
        }
    }
}
doSort($cards, $result);
print_r($result);

Php output demo Php输出演示

That will give you: 那会给你:

Array
(
    [0] => Array
        (
            [from] => Madrid
            [to] => Barcelona
        )

    [1] => Array
        (
            [from] => Barcelona
            [to] => Gerona Airport
        )

    [2] => Array
        (
            [from] => Gerona Airport
            [to] => Stockholm
        )

    [3] => Array
        (
            [from] => Stockholm
            [to] => New York JFK
        )

)

here is a way. 这是一种方式。 Finding the start, and search the path, by iteration : 通过迭代找到开始并搜索路径:

// 1. Find the start
$start = null;
$starts = array_column($cards,'from');
$ends = array_column($cards,'to');
foreach ($cards as $card) {
    if (!in_array($card['from'], $ends)) $start=$card;
}
// 2. Find way
$way=[$start];
while (count($cards)>1) {
    $found = false ;
    foreach ($cards as $k => $card) {
        if ($card['from'] == $start['to']) {
            $way[] = $card;
            $start = $card;
            unset($cards[$k]);
            $found = true ;
            break 1;
        }
    }
    if (!$found) { echo "Impossible to use all cards.\n" ; break ; }
}
print_r($way);

Outputs: 输出:

    Array (
    [0] => Array ([from] => Madrid [to] => Barcelona )
    [1] => Array ([from] => Barcelona [to] => Gerona Airport )
    [2] => Array ([from] => Gerona Airport [to] => Stockholm )
    [3] => Array ([from] => Stockholm [to] => New York JFK )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM