使用 PHP 中的字符串对关联数组进行排序

Sorting an associative array with a string in PHP

我正在处理关联数组的 php 问题并尝试对其进行排序。我的看起来像这样:


[2021-07-16T10:45:23-04:00] => Array
    (
        [0] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
        [1] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
        [2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
        [3] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Oranges
    )

[2021-07-16T10:45:24-04:00] => Array
    (
        [0] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
        [1] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
        [2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
        [3] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
        [4] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Oranges
        [5] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
        [6] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Apples
    )

我想对其进行排序,使其看起来像这样:

[2021-07-16T10:45:23-04:00] => Array
    (
        [0] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Oranges
        [1] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
        [2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
        [3] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
    )

[2021-07-16T10:45:24-04:00] => Array
    (
        [0] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Apples
        [1] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
        [2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
        [3] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Oranges
        [4] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
        [5] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
        [6] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981Apples
    )

我想过沿着|爆炸然后取 0.0.0.111 并将其与其他所有内容进行比较。我试过的是这样的:

  foreach ($test_array as $info) {
    asort($info);
  }

虽然我比较了整体,但我意识到:0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples

而不是第一部分 0.0.1.222。感谢您的帮助。

尝试 array_map('sort', $yourMainArray); 或 运行 你自己在地图上的回调,甚至一个简单的循环和排序就可以了。