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

Sorting arrays based on words contained in string

提问人:Zyfella 提问时间:5/19/2022 最后编辑:Zyfella 更新时间:5/19/2022 访问量:47

问:

我见过很多对数组进行排序的例子,包括 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 
        )

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

我真的尝试了很多不同的代码,但没有一个有帮助。这是我能提供的,将有助于你的理解。

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 排序

评论

0赞 CBroe 5/19/2022
这回答了你的问题吗?如何在PHP中对数组和数据进行排序?
0赞 CBroe 5/19/2022
自定义回调函数需要根据所有条件逐个比较两个项目。如果只有第一个包含而第二个不包含,那么您将第一个视为比第二个“高”,反之亦然。如果它们都包含它,或者都不包含它 - 那么你继续检查同样的东西......TonerDrum kit
0赞 Zyfella 5/19/2022
我是这样写的: ' 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']]; });' 它就像我想要的一样工作,但是,并非每个碳粉黑都有相同的挖掘,我该如何制作它,以便它只寻找字符串是否存在?如何插入正则表达式?preg_match(“/碳粉/”, $products)
0赞 CBroe 5/19/2022
“how can i insert a RegEx” - 你不能将正则表达式“插入”到那种数组中。您需要分别确定这两个项目的此 0/1/2 值。

答:

1赞 Anisur Rahman 5/19/2022 #1
<?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
        )

)