sort json 导致 foreach 循环
sort json results in foreach loop
我是 运行 一个用于显示 json 结果的 foreach 循环,当满足某些条件时,我想按 name
字段对它们进行排序。我正在尝试 usort()
,但似乎无法弄清楚。
JSON:
{
"Shawn Taylor":{
"name":"Shawn Taylor",
"title":"",
"photo_url":"house_165 (1).jpg",
},
"Another Name": {
"name":"Another Name",
"title":"Title is here",
"photo_url":"Person.jpg",
}
}
PHP:
$data_json = file_get_contents('data.json');
$data_array = json_decode($data_json, true);
$i = 0;
foreach($data_array as $key => $person){
if($person['title'] == 'some title'){
include('card.php');
if(++$i % 4 === 0) {
echo '<div class="clearfix"></div>'; // inserts a clearfix every 4 cards
}
}
}
所以这个 returns 是我期望的所有结果,但没有排序。我已经尝试了几种不同的 usort() 方法,但我的脸上摔得很厉害:) 请帮忙!
首先使用json_decode转换为php数组,将关联数组的标志设置为TRUE $myarr = json_decode($array, TRUE)
尝试自定义 usort
// Sort the multidimensional array
usort($myarr, "custom_sort");
// Define the custom sort function
function custom_sort($a,$b) {
return $a['name']>$b['name'];
}
希望对您有所帮助。
您的 json 格式不正确。每个 JPG 项目后有几个额外的逗号。下面已删除。
然后,json_decode
json 字符串到 PHP 关联数组,并且,由于您将名称用作 json 索引,ksort
(键排序)结果数组。
$json_string = '{
"Shawn Taylor":{
"name":"Shawn Taylor",
"title":"",
"photo_url":"house_165 (1).jpg"
},
"Another Name": {
"name":"Another Name",
"title":"Title is here",
"photo_url":"Person.jpg"
}
}';
$data_array = json_decode($json_string, true);
ksort($data_array);
// the remaining code
A print_r
在 ksort
之后显示:
Array
(
[Another Name] => Array
(
[name] => Another Name
[title] => Title is here
[photo_url] => Person.jpg
)
[Shawn Taylor] => Array
(
[name] => Shawn Taylor
[title] =>
[photo_url] => house_165 (1).jpg
)
)
如果您需要按嵌套索引排序,并希望维护关联数组,请使用 uasort
:
uasort($data_array, 'sort_name_index');
function sort_name_index($a, $b) {
return $a['name'] > $b['name'];
}
我是 运行 一个用于显示 json 结果的 foreach 循环,当满足某些条件时,我想按 name
字段对它们进行排序。我正在尝试 usort()
,但似乎无法弄清楚。
JSON:
{
"Shawn Taylor":{
"name":"Shawn Taylor",
"title":"",
"photo_url":"house_165 (1).jpg",
},
"Another Name": {
"name":"Another Name",
"title":"Title is here",
"photo_url":"Person.jpg",
}
}
PHP:
$data_json = file_get_contents('data.json');
$data_array = json_decode($data_json, true);
$i = 0;
foreach($data_array as $key => $person){
if($person['title'] == 'some title'){
include('card.php');
if(++$i % 4 === 0) {
echo '<div class="clearfix"></div>'; // inserts a clearfix every 4 cards
}
}
}
所以这个 returns 是我期望的所有结果,但没有排序。我已经尝试了几种不同的 usort() 方法,但我的脸上摔得很厉害:) 请帮忙!
首先使用json_decode转换为php数组,将关联数组的标志设置为TRUE $myarr = json_decode($array, TRUE)
尝试自定义 usort
// Sort the multidimensional array
usort($myarr, "custom_sort");
// Define the custom sort function
function custom_sort($a,$b) {
return $a['name']>$b['name'];
}
希望对您有所帮助。
您的 json 格式不正确。每个 JPG 项目后有几个额外的逗号。下面已删除。
然后,json_decode
json 字符串到 PHP 关联数组,并且,由于您将名称用作 json 索引,ksort
(键排序)结果数组。
$json_string = '{
"Shawn Taylor":{
"name":"Shawn Taylor",
"title":"",
"photo_url":"house_165 (1).jpg"
},
"Another Name": {
"name":"Another Name",
"title":"Title is here",
"photo_url":"Person.jpg"
}
}';
$data_array = json_decode($json_string, true);
ksort($data_array);
// the remaining code
A print_r
在 ksort
之后显示:
Array
(
[Another Name] => Array
(
[name] => Another Name
[title] => Title is here
[photo_url] => Person.jpg
)
[Shawn Taylor] => Array
(
[name] => Shawn Taylor
[title] =>
[photo_url] => house_165 (1).jpg
)
)
如果您需要按嵌套索引排序,并希望维护关联数组,请使用 uasort
:
uasort($data_array, 'sort_name_index');
function sort_name_index($a, $b) {
return $a['name'] > $b['name'];
}