3

好吧,我的问题很复杂。

我有那个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 时。

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

提前致谢

4

3 回答 3

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

我已经尝试过它及其工作

于 2013-03-12T11:08:56.687 回答
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

于 2013-03-12T10:53:59.153 回答
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);
?>

这可以帮助你。

于 2013-03-12T10:59:38.300 回答