如何在Laravel中通过json属性对集合进行排序

时间:2017-10-25 13:12:56

标签: laravel

在我的代码中,我有一个集合对象。集合对象的一部分结构如下

"id" => 1
"rate" => "{"p_1":"1","p_2":"2","p_3":"2.3","p_4":"3.5"}"
"currency" => 1
"desc" => "TEST"

在上面的例子中,我有一个名为rate的密钥,它有一个json字符串作为值。

我想要做的是按p_1键中包含的值range对集合对象进行排序。

2 个答案:

答案 0 :(得分:2)

您必须在订购前解码JSON字符串:

Sub Multi_FindReplaceALL_pvc_replace_new()
'PURPOSE: Find & Replace a list of text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long

fndList = Array("2 ft", "PVC", "390")
rplcList = Array("2 ft", "PVC", "290")

'Loop through each item in Array lists
 For x = LBound(fndList) To UBound(fndList)
'Loop through each worksheet in ActiveWorkbook
  For Each sht In ActiveWorkbook.Worksheets
    sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
      SearchFormat:=False, ReplaceFormat:=False
  Next sht

  Next x

End Sub

答案 1 :(得分:1)

使用sortBy()对laravel中的集合进行排序

$collection->sortBy(function($item) {
  return $item->rate->p_1;
});

此处$collection是您的laravel结果,默认情况下,您可以使用collect方法显式转换它

查看docs了解详情