在PHP排序JSON或多维数组数据 [英] sort json or multi dimensional array data in php

查看:787
本文介绍了在PHP排序JSON或多维数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了网站,并有相关的对这个问题的答案吨的。但我学习PHP。因此,它是相当困难,我听不懂。
不管怎么说,

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;

我使用AJAX来从数据库中的数据。一切工作正常,但我想通过价格对数据进行排序。我不能硬code,因为我希望用户选择相同。
有嵌套MySQL的查询,这是不可能从MySQL查询中的数据进行排序。由于数据来自多个查询的到来。

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.

我发现SO 我如何排序这个问题在PHP 多维数组
我不知道这会在我的情况下工作或没有。

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.

感谢

编辑:
数据库表结构。

database table structure.

用户ID,标题,蛞蝓,电子邮件,移动,FNAME,LNAME,地址,PIC 从用户表的到来。 下载,usrtitle,公司,价格从概要表来。 appliedon 来自何处申请表。

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.

第一次查询运行。该用户ID的用户和个人资料表用于获取详细信息。我不知道我可以使用排序依据子句中的数据在这里进行排序。

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.

推荐答案

您可以使用 usort 并定义自己的排序功能:

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

如果您希望通过名称(或其他字符串)命令,你可以使用 strnatcmp 例如

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"]);
});

这篇关于在PHP排序JSON或多维数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆