1

I have checked the site and there are tons of answers relevant to this question. But i am learning PHP. So, it is quite hard for me to understand. Anyways,

$data = array();
$data['count'] = $result->num_rows;
...
...
$data['content'][] = array(
                    "userid" => $userid,
                    "title" => $title,
                    "slug" => $slug,
                    "download" => $download,
                    "usrtitle" => $usrtitle,
                    "company" => $company,
                    "period" => $period,
                    "email" => $email,
                    "mobile" => $mobile,
                    "fname" => $fname,
                    "lname" => $lname,
                    "address" => $address,
                    "pic" => $pic,
                    "appliedon" => date("d-M-Y", strtotime($appliedon)),
                    "price" => $price
                    );
echo json_encode($data);exit;

I am using ajax to fetch data from database. Everything is working fine but i want to sort data by price. I can't hard code it because i want user to select the same. There are nested mysql queries and it is not possible to sort the data from mysql query. Because data are coming from multiple queries.

Please advise how can i sort the data.

I found this question on SO How do I Sort a Multidimensional Array in PHP I am not sure this will work in my case or not.

Thanks

Edit: database table structure.

userid, title, slug,email,mobile,fname,lname,address,pic is coming from users table. download, usrtitle, company,price coming from profile table. appliedon coming from apply table.

First query run on appliedon table from there i get userid. this userid used in users and profile table to fetch the details. I am not sure i can use orderby clause to sort the data here.

Community
  • 1
  • 1
Roxx
  • 3,738
  • 20
  • 92
  • 155

2 Answers2

1

PHP built in function array_multisort is work for this case.

1.Get an array of prices coming from $data['content']

$sort_arr = array();
foreach($data['content'] as $item) {
    $sort_arr[] = $item['price'];
}

2.Use array_multisort to sort $data['content'] by price in descent order:

//if you want ascending order replace `SORT_DESC` by `SORT_ASC`
array_multisort($sort_arr, SORT_NUMERIC, SORT_DESC, $data['content'])

More detail of array_multisort usage, see official documentation: http://php.net/manual/en/function.array-multisort.php

Kevin Yan
  • 1,236
  • 11
  • 19
1

You can use usort and define own sort function:

$data = [
    ["name" => "A", "price" => 54],
    ["name" => "B", "price" => 102],
    ["name" => "C", "price" => 4],
    ["name" => "D", "price" => 76],
];

echo("UNSORTED <br>\n");
print_r($data);

usort($data, function($a, $b) {
    return $a["price"] - $b["price"];
});
echo("\n<br>\n<br>SORTED <br>\n");
print_r($data);

If you would like to order by names (or other string), you can use strnatcmp for example.

usort($data, function($a, $b) {
    return strnatcmp($a["name"], $b["name"]);
});
Buksy
  • 11,571
  • 9
  • 62
  • 69