Home > Mobile >  Sorting arrays based on words contained in string
Sorting arrays based on words contained in string

Time:05-19

I've seen a lot of examples of sorting arrays, with DESC and ASC, alphabetic, and more, but I didn't find any that could help me in this situation. I have a set of large arrays that I'm only going to show the important part of it. Here are the arrays:

[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 
        )

what I would like to do is to have arrays with articleName that has "Toner" in the string to be shown first (priority 0) and then articleName that has "Drum kit" to be shown after "Toner" (priority 1) and then "Original" (priority 2)

i really tried a lot of this different code but none of them helped. this is what i can offer that will be helpful for ur understanding.

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 
            )

UPDATE: i wrote this:

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']];

 });

it works.

CodePudding user response:

<?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);

Output:

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
        )

)
  • Related