根据字符串中包含的单词对数组进行排序

Sorting arrays based on words contained in string

我看过很多数组排序的示例,包括 DESC 和 ASC、字母顺序等等,但我没有找到任何可以帮助我解决这种情况的示例。 我有一组大型阵列,我只打算展示其中的重要部分。 以下是数组:

[0] => Array
        (
            [articleID] => 3166
            [articleName] => Drum Kit MultiPack 
        )

[1] => Array
        (
            [articleID] => 2073
            [articleName] => Toner black TN-241BK 
        )

[2] => Array
        (
            [articleID] => 3241
            [articleName] => Toner black TN-241BK 
        )

[3] => Array
        (
            [articleID] => 3242
            [articleName] => Original Kit colour 
        )

我想要做的是让 articleName 的数组在字符串中首先显示“Toner”(优先级 0),然后在“Toner”之后显示具有“Drum kit”的 articleName(优先级 1) 然后是“原始”(优先级 2)

我确实尝试了很多这种不同的代码,但 none 有帮助。这是我能提供的,将有助于您的理解。

public function indexAction()
    {
    $products = array() // this variable contains all the original arrays that i showed above.
    $priorities = array('Toner'=>0,'Drum Kit'=>1,'Original'=> 2); //$products array should be set to these priorities

    function sorting ($a, $b) { 
    //magic code
       }

    uasort($products, 'sorting');
    print_r( $products);

    }



   the outcome should look like:

    [0] => Array
            (
                [articleID] => 2073
                [articleName] => Toner black TN-241BK 
            )
    
    [1] => Array
            (
                [articleID] => 3241
                [articleName] => Toner black TN-361MK 
            )
   [2] => Array
            (
                [articleID] => 3166
                [articleName] => Drum Kit MultiPack 
            )
    
    [3] => Array
            (
                [articleID] => 3242
                [articleName] => Original Kit colour 
            )

更新: 我写了这个:

usort($products, function ($a, $b) {
$productArray = array('Toner Black TN-241BK'=>0,'Drum Kit MultiPack'=>1,'Original Kit colour'=> 2);

return $productArray[$a['articleName']] <=> $productArray[$b['articleName']];

 });

有效。

<?php

// Text to be match => order
$sortOrders = [
  'Toner' => 1,
  'Drum Kit' => 2,
  'Original' => 3
];

// Data collection
$data = [
  [
      'articleID' => 3166,
      'articleName' => 'Drum Kit MultiPack' 
  ],
  [
      'articleID' => 2073,
      'articleName' => 'Toner black TN-241BK'
  ],
  [
      'articleID' => 3241,
      'articleName' => 'Toner black TN-241BK'
  ],
  [
      'articleID' => 3242,
      'articleName' => 'Original Kit colour'
  ]
];

usort($data, function($data1, $data2) use($sortOrders){

  foreach( $sortOrders as $key => $sortOrder ){

    // Determine sort order
    if (strpos($data1['articleName'], $key) !== false) {
      $orderForData1 = $sortOrder;
    }
    if (strpos($data2['articleName'], $key) !== false) {
      $orderForData2 = $sortOrder;
    }

  }
  return $orderForData1 <=> $orderForData2;

});

print_r($data);

输出:

Array
(
    [0] => Array
        (
            [articleID] => 3241
            [articleName] => Toner black TN-241BK
        )

    [1] => Array
        (
            [articleID] => 2073
            [articleName] => Toner black TN-241BK
        )

    [2] => Array
        (
            [articleID] => 3166
            [articleName] => Drum Kit MultiPack
        )

    [3] => Array
        (
            [articleID] => 3242
            [articleName] => Original Kit colour
        )

)