获取json并使用多个参数进行排序

时间:2013-03-12 10:47:02

标签: php json

嗯,我的问题很复杂。

我有那个json文件:

{
"books": [
    {
        "book": [
            {
                "Title": "Java How to Program",
                "Author": "Deitel & Deitel",
                "Edition": "2007"
            }
        ]
    },
    {
        "book": [
            {
                "Title": "Patterns of Enterprise Application Architecture",
                "Author": "Martin Fowler",
                "Edition": "2002"
            }
        ]
    },
    {
        "book": [
            {
                "Title": "Head First Design Patterns",
                "Author": "Elisabeth Freeman",
                "Edition": "2004"
            }
        ]
    },
    {
        "book": [
            {
                "Title": "Internet & World Wide Web: How to Program",
                "Author": "Deitel & Deitel",
                "Edition": "2007"
            }
        ]
    }
]

}

在我的PHP中我有这个:

    $file = file_get_contents('books.json');
    $json = json_decode($file, true);

如何按多个规则对数组进行排序?例如:

echo sort_my_array('Title', ASC, 'Author', DESC, $json);

我尝试了很多方法,但我认为我的错误是在我尝试使用array_multidimensional时。

有人可以向我解释如何创建这个吗?

提前致谢

3 个答案:

答案 0 :(得分:5)

试试这个

    $file = file_get_contents('books.json');
    $json = json_decode($file, true);
   $json = array_map(function($a) { return $a['book'][0]; }, $json['books']);
 foreach ($json as $key => $row) {
    $title[$key] = $row['Title'];
   $author[$key] = $row['Author'];
 }
 array_multisort($title, SORT_ASC,  $author, SORT_DESC, $json);
 echo "<pre>";
 print_r($json);
 echo "</pre>";

我已尝试过它的工作

答案 1 :(得分:1)

使用array_multisort

<?php
$json = '{
"books": [
{
    "book": [
        {
            "Title": "Java How to Program",
            "Author": "Deitel & Deitel",
            "Edition": "2007"
        }
    ]
},
{
    "book": [
        {
            "Title": "Patterns of Enterprise Application Architecture",
            "Author": "Martin Fowler",
            "Edition": "2002"
        }
    ]
},
{
    "book": [
        {
            "Title": "Head First Design Patterns",
            "Author": "Elisabeth Freeman",
            "Edition": "2004"
        }
    ]
},
{
    "book": [
        {
            "Title": "Internet & World Wide Web: How to Program",
            "Author": "Deitel & Deitel",
            "Edition": "2007"
        }
    ]
}
]
}';

$json = json_decode($json,true);
echo "<pre>";
//print_r($json);

$sort = array();
foreach($json['books'] as $k=>$v) {
$sort['title'][$k] = $v['book'][0]['Title'];
$sort['author'][$k] = $v['book'][0]['Author'];
}
//print_r($sort);
array_multisort($sort['title'], SORT_DESC, $sort['author'], SORT_ASC,$json['books']);
print_r($json['books']);

输出http://codepad.viper-7.com/dXjDLg

答案 2 :(得分:1)

使用array_multisort - 对多维或多维数组进行排序。

示例:

<?php
// Obtain a list of columns
foreach ($book as $key => $row) {
    $Title[$key]  = $row['Title'];
    $Author[$key] = $row['Author'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($Title, SORT_ASC, $Author, SORT_DESC, $book);
?>

这可能对你有所帮助。