按成本和名称对数组进行排序

时间:2015-12-17 12:51:25

标签: php arrays web-services

  {
m: "1",
total: "6",
r: [
{
job_id: "472",
category: "plumbing",
job_cost: "350",
posted_on: "17 Dec 2015",
completed_on: "17 Dec 2015",
job_final_status: "1"
},
{
job_id: "459",
category: "electrical",
job_cost: "600",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "457",
category: "electrical",
job_cost: "1000",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "456",
category: "carpentry",
job_cost: "350",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "454",
category: "electrical",
job_cost: "450",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "433",
category: "plumbing",
job_cost: "400",
posted_on: "15 Dec 2015",
completed_on: "15 Dec 2015",
job_final_status: "1"
}
]
}

以上是我的网络服务的回复。 我想要数组(' r')结果,首先按类别名称排序,如果类别名称相同,那么该类别的结果应按job_cost排序更高到更低。

所以我想要的结果如下:

{
m: "1",
total: "6",
r: [
{
job_id: "456",
category: "carpentry",
job_cost: "350",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "457",
category: "electrical",
job_cost: "1000",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "459",
category: "electrical",
job_cost: "600",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "454",
category: "electrical",
job_cost: "450",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "433",
category: "plumbing",
job_cost: "400",
posted_on: "15 Dec 2015",
completed_on: "15 Dec 2015",
job_final_status: "1"
},
{
job_id: "472",
category: "plumbing",
job_cost: "350",
posted_on: "17 Dec 2015",
completed_on: "17 Dec 2015",
job_final_status: "1"
}
]
}

3 个答案:

答案 0 :(得分:1)

首先,在MYSQL之后触发FILED查询以排序uasort()之后,您应该使用数组按值排序DECLARE @dt DateTime = GetDate(); DECLARE @sqlCommand NVARCHAR(MAX); DECLARE @tmpsqlCommand NVARCHAR(100); DECLARE curTbls CURSOR FOR SELECT 'SELECT TOP 100 CreatedDate, ' + FunctionToGetAllColumnsForTableIntoOneBigOne(name) as colMain + ' FROM ' + name + ' UNION ALL ' FROM Sys.tables; OPEN curTbls FETCH NEXT FROM curTbls INTO @sqlCommand; WHILE @@FETCH_STATUS = 0 BEGIN FETCH NEXT FROM curTbls INTO @tmpsqlCommand; SET @sqlCommand = @sqlCommand + @tmpsqlCommand; END; CLOSE curTbls; DEALLOCATE curTbls; SET @sqlCommand = @sqlCommand + ' SELECT GetDate(),' SET @sqlCommand = 'SELECT TOP 100 * FROM ( '+ @sqlCommand + ') sub_q ORDER BY CreatedDate DESC' --SELECT @sqlCommand EXECUTE sp_executesql @sqlCommand 函数。 试试这些,可能会对你有帮助..

答案 1 :(得分:1)

这就是我解决问题的方法。

首先使用以下方法解码信息:

 $r = json_decode($yourJSONstring);

然后你可以创建一个空数组来填充发生的类别:

$occurring = array();

然后你可以通过循环它们来组合相同类别的数组:

foreach ($r as $item) {
    if (isset($occurring [$item['category']])) {
        $occurring [$item['category']][] = $item;
    } else {
        $occurring [$item['category']] = array();
        $occurring [$item['category']][] = $item;
    }
}

然后你可以对这样的东西进行排序:

foreach ($occurring as $key => $item) {
    $occurring[$key] = Classname::array_sort_by_column($item,"job_cost",SORT_DESC);
}

使用这样的函数:

static public function sort_by_column($sortArray, $columnName, $sortDirection = SORT_ASC) {
    $sortCol = array();

    foreach ($sortArray as $key => $row) {
      $sortCol [$key] = $row[$columnName];
    }

    array_multisort($sortCol , $sortDirection, $sortArray);

    return $sortArray;
}

我希望这能解决你的问题:)

答案 2 :(得分:1)

首先,您应该触发MYSQL查询以进行FILED排序,然后您应该使用数组按值相应地uasort()函数。试试这些,可能会对你有帮助..