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.

6
  • 4
    sort the data in mysql query itself Commented Apr 22, 2016 at 4:44
  • please show your query. It will give you sorted data. Commented Apr 22, 2016 at 4:45
  • There are nested mysql queries and it is not possible to sort the data in mysql query. Because data are coming from multiple queries. Commented Apr 22, 2016 at 4:59
  • ORDER BY clause is used to sort particular columns inside database queries.. Commented Apr 22, 2016 at 5:04
  • @MohammedShafeek i have updated the question. Hope this will make more sense. Commented Apr 22, 2016 at 5:20

2 Answers 2

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

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

2 Comments

Thanks for your answer. Voted up.
Didn't tried yet. As i am away from my machine. Will check and let you know.
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"]);
});

2 Comments

Thanks for your answer. Voted Up.
Is this what you've been lookin for?

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.