0

I have a variable $_SESSION["cart_item"] which stores cart items by array like given below -

Array
( 
    [0] => Array
        (
            [company] => ABC company 
            [good] => Laptop 
            [price] => 100 
            [ship] => 20 
        )

    [1] => Array
        (
            [company] => ABC company 
            [good] => PC 
            [price] => 20
            [ship] => 70 
        )

    [2] => Array
        (
            [company] => DELL PLC 
            [good] => Hard Drive 
            [price] => 500 
            [ship] => 50 
        )

    [3] => Array
        (
            [company] => ABC company 
            [good] => Bag 
            [price] => 30
            [ship] => 40 
        )

)

I want to get the sorted data by company and the [ship] is highest value.

  • And you want to Sort by?...... – Vinay Jun 06 '20 at 10:21
  • Does this answer your question? [How should I sort this array by key with usort?](https://stackoverflow.com/questions/5915682/how-should-i-sort-this-array-by-key-with-usort) – Vinay Jun 06 '20 at 10:22

1 Answers1

1
$sortdata = sort2d_bycolumn($data,'ship',SORT_DESC);
function sort2d_bycolumn($array, $column, $method)
{
    foreach ($array as $key => $row) {
        $narray[$key]  = $row[$column]; 
    }
    array_multisort($narray, $method, $array);
    return $array;
}

sort2d_bycolumn is the sorting function $array is the array you want to sort $column is the column that array needed to be sorted on $method value will be either SORT_ASC or SORT_DESC

Shafeeque TP
  • 361
  • 1
  • 16