0

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.

4
  • Does this answer your question? How can I sort arrays and data in PHP? Commented May 19, 2022 at 9:49
  • Your custom callback function needs to compare two items on all your criteria, one after the other. If only the first one contains Toner and the second one doesn't, then you treat the first one as "higher" than the second, vice versa the other way around. If they both contain it, or both don't contain it - then you proceed checking the same thing for Drum kit ... Commented May 19, 2022 at 9:51
  • 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 just as i want but, not every Toner Black has the same endigs, how do i make it so it only looks for if string exists ? how can i insert a RegEx ? preg_match("/Toner/", $products) Commented May 19, 2022 at 10:43
  • "how can i insert a RegEx" - you can not "insert" a regex into that kind of array. You will need to determine this 0/1/2 value for both items separately. Commented May 19, 2022 at 11:16

1 Answer 1

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

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.