skip to Main Content

PHP

Format : $salesData["suppliers"][$supplierkey]['sales'][$salesPeriod]['sales']

I want to sort (desc):

The key $salesData["suppliers"][$supplierkey] by the numerical value populated in $salesData["suppliers"][$supplierkey]['sales'][0]['cost']

So the following example:

$salesData["suppliers"][Dong]
$salesData["suppliers"][Ding]

Would be changed to:

$salesData["suppliers"][Ding]
$salesData["suppliers"][Dong]

Because

$salesData["suppliers"][Ding][sales][0][cost] = 231.0600
$salesData["suppliers"][Dong][sales][0][cost] = 92.8900

Is it possible to do it on multidimension arrays? I assume I need to use array_multisort somehow?

Any advise would be appreciated

[
  {
    suppliers: {
      Dong: {
        ID: 3663313744756234943,
        sales: [
          { cost: 92.8900, sales: 180.869565 },
          { cost: 12.9900, sales: 394.782609 },
          {
            locations: {
              salesRow: 18,
              costRow: 19,
              GPRow: 20,
              marginRow: 21,
              pSalesChangeRow: 22,
              pGPChangeRow: 23,
              ptotalSalesRow: 24,
              ptotalGPRow: 25,
            },
          },
        ],
      },
      Ding: {
        ID: 3678436471493790070,
        sales: [
          { cost: 231.0600, sales: 95.565217 },
          { cost: 26.8200, sales: 85.130435 },
          {
            locations: {
              salesRow: 28,
              costRow: 29,
              GPRow: 30,
              marginRow: 31,
              pSalesChangeRow: 32,
              pGPChangeRow: 33,
              ptotalSalesRow: 34,
              ptotalGPRow: 35,
            },
          },
        ],
      },
    },
  },
]

The other examples on this site show 1 level down sorting but not multi levels down.

I tried :

Foreach($salesData["suppliers"] as $supplierkey=>$id) {

        $keys = array_column($salesData["suppliers"], 'cost'); 
        
        array_multisort($keys, SORT_ASC, $new); 
        
        var_dump($new);

        die();
}

but it provides an error I don’t think I am getting :’cost’ right?

2

Answers


  1. This should be easy. 1st you store all the costs in an array and then sort that costs array. Now reorder the main suppliers array based on the sorted costs array.

    Something like the following:

    function sortByCost($salesData)
    {
        // Storing the costs
        $costs = [];
        foreach ($salesData["suppliers"] as $supplierKey => $supplierData) {
            $costs[$supplierKey] = $supplierData["sales"][0]["cost"];
        }
    
        // Sort the costs array
        arsort($costs);
    
        // Reordering the suppliers based on the sorting of cost array keys
        $sortedSuppliers = [];
        foreach ($costs as $supplierKey => $cost) {
            $sortedSuppliers[$supplierKey] = $salesData["suppliers"][$supplierKey];
        }
    
        $salesData["suppliers"] = $sortedSuppliers;
    
        return $salesData;
    }
    
    Login or Signup to reply.
  2. You can use uasort to define yourself the sorting condition and to keep the named keys of your array.

    foreach($salesData as &$item) {
        uasort(
            $item['suppliers'],
            fn($a, $b) => $b['sales'][0]['cost'] <=> $a['sales'][0]['cost']
        );
    }
    

    demo

    Note that you have to use a reference in the foreach loop (otherwise you will sort only a copy of your item and not the item itself in your array).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search