duanjie9630 2017-02-10 17:21
浏览 46
已采纳

按子子数组值排序数组

How do I sort the following array.

the top level array meter type :

"With a Smart meter", "With an old-fashioned meter

inside those there are various tariffs which are also arrays. But each tariff has a total. I need to sort the tariffs inside each meter type array by the total with lowest first in the array.

How do I do this bearing in mind I dont know the tariff names each time the arrays are generated. For example the tarriff name 873.03195-SDD-E1R-VAR-0002_SDD-G1R-VAR-0002 could be different each time.

array:2 [
 "With a Smart meter" => array:3 [
  "873.03195-SDD-E1R-VAR-0002_SDD-G1R-VAR-0002" => array:4 [
  "electricity" => array:4 [
    "code" => "SDD-E1R-VAR-0002"
    "name" => "Smart Go with the flow"
    "costs" => array:4 [
      "year" => "464.13"
      "month" => "38.68"
      "week" => "8.93"
      "savings" => 27.0
    ]
    "details" => array:4 [
      "name" => "Smart Go with the flow"
      "unitRate" => array:3 [
        "standard" => 12.4992
        "night" => null
        "day" => null
      ]
      "standingCharge" => 21.0
      "comparisonRate" => 14.97
    ]
  ]
  "gas" => array:4 [
    "code" => "SDD-G1R-VAR-0002"
    "name" => "Smart Go with the flow - Gas"
    "costs" => array:4 [
      "year" => "408.91"
      "month" => "34.08"
      "week" => "7.86"
      "savings" => 160.0
    ]
    "details" => array:4 [
      "name" => "Smart Go with the flow - Gas"
      "unitRate" => 2.5998
      "standingCharge" => 22.995
      "comparisonRate" => 3.27
    ]
  ]
  "display" => array:6 [
    "code" => "SDD-E1R-VAR-0002_SDD-G1R-VAR-0002"
    "name" => "Smart Go with the flow"
    "idPaymentMethod" => "4"
    "textPaymentMethod" => "Smart Direct Debit"
    "costs" => array:4 [
      "year" => "873.03"
      "month" => "72.75"
      "week" => "16.79"
      "savings" => 186.0
    ]
    "podCopy" => array:2 [
      "subHeading" => "Go with the flow"
      "listContent" => """
        <ul>

        \t<li><span style="font-family: Arial, sans-serif; font-size: 14px;">Say goodbye to estimated billing with a TOTO smart meter</span></li>

        \t<li><span style="font-family: Arial, sans-serif; font-size: 14px;">Keep track of your usage with our FREE Smart Meter and in-home display</span></li>

        </ul>
        """
    ]
  ]
  "total" => "873.03"
]
"1034.19225-SDD-E1R-VAR-0004_SDD-G1R-VAR-0004" => array:4 [
  "electricity" => array:4 [
    "code" => "SDD-E1R-VAR-0004"
    "name" => "TOTO Smart Evergreen Saver"
    "costs" => array:4 [
      "year" => "547.32"
      "month" => "45.61"
      "week" => "10.53"
      "savings" => -57.0
    ]
    "details" => array:4 [
      "name" => "TOTO Smart Evergreen Saver"
      "unitRate" => array:3 [
        "standard" => 15.183
        "night" => null
        "day" => null
      ]
      "standingCharge" => 21.0
      "comparisonRate" => 17.66
    ]
  ]
  "gas" => array:4 [
    "code" => "SDD-G1R-VAR-0004"
    "name" => "TOTO Smart Evergreen Saver - Gas"
    "costs" => array:4 [
      "year" => "486.87"
      "month" => "40.57"
      "week" => "9.36"
      "savings" => 82.0
    ]
    "details" => array:4 [
      "name" => "TOTO Smart Evergreen Saver - Gas"
      "unitRate" => 3.2235
      "standingCharge" => 22.995
      "comparisonRate" => 3.89
    ]
  ]
  "display" => array:6 [
    "code" => "SDD-E1R-VAR-0004_SDD-G1R-VAR-0004"
    "name" => "TOTO Smart Evergreen Saver"
    "idPaymentMethod" => "4"
    "textPaymentMethod" => "Smart Direct Debit"
    "costs" => array:4 [
      "year" => "1,034.19"
      "month" => "86.18"
      "week" => "19.89"
      "savings" => 25.0
    ]
    "podCopy" => array:2 [
      "subHeading" => "Evergreen Saver"
      "listContent" => """
        <ul>

        \t<li>No contract, no end date, no hassle.</li>

        \t<li>Say goodbye to estimated billing with a TOTO Smart meter and FREE in home display.</li>

        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>

        \t<li>Prompt friendly service from the friendly TOTO team.<br />

        \t</li>

        </ul>
        """
    ]
  ]
  "total" => "1,034.19"
]
"894.71025-SDD-E1R-VAR-0003_SDD-G1R-VAR-0003" => array:4 [
  "electricity" => array:4 [
    "code" => "SDD-E1R-VAR-0003"
    "name" => "TOTO Smart Discount Variable Saver"
    "costs" => array:4 [
      "year" => "485.80"
      "month" => "40.48"
      "week" => "9.34"
      "savings" => 5.0
    ]
    "details" => array:4 [
      "name" => "TOTO Smart Discount Variable Saver"
      "unitRate" => array:3 [
        "standard" => 13.1985
        "night" => null
        "day" => null
      ]
      "standingCharge" => 21.0
      "comparisonRate" => 15.67
    ]
  ]
  "gas" => array:4 [
    "code" => "SDD-G1R-VAR-0003"
    "name" => "TOTO Smart Discount Variable Saver - Gas"
    "costs" => array:4 [
      "year" => "408.91"
      "month" => "34.08"
      "week" => "7.86"
      "savings" => 160.0
    ]
    "details" => array:4 [
      "name" => "TOTO Smart Discount Variable Saver - Gas"
      "unitRate" => 2.5998
      "standingCharge" => 22.995
      "comparisonRate" => 3.27
    ]
  ]
  "display" => array:6 [
    "code" => "SDD-E1R-VAR-0003_SDD-G1R-VAR-0003"
    "name" => "TOTO Smart Discount Variable Saver"
    "idPaymentMethod" => "4"
    "textPaymentMethod" => "Smart Direct Debit"
    "costs" => array:4 [
      "year" => "894.71"
      "month" => "74.56"
      "week" => "17.21"
      "savings" => 165.0
    ]
    "podCopy" => array:2 [
      "subHeading" => "Discount Variable Saver"
      "listContent" => """
        <ul>

        \t<li>Discounted rate for 12 months.</li>

        \t<li>Say goodbye to estimated billing with a TOTO Smart meter and FREE in home display.</li>

        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>

        \t<li>12 month contract.</li>

        </ul>
        """
    ]
  ]
  "total" => "894.71"
]
]
"With an old-fashioned meter" => array:3 [
"873.03195-LDD-E1R-VAR-0002_LDD-G1R-VAR-0002" => array:4 [
  "electricity" => array:4 [
    "code" => "LDD-E1R-VAR-0002"
    "name" => "Go with the flow 2"
    "costs" => array:4 [
      "year" => "464.13"
      "month" => "38.68"
      "week" => "8.93"
      "savings" => 27.0
    ]
    "details" => array:4 [
      "name" => "Go with the flow 2"
      "unitRate" => array:3 [
        "standard" => 12.4992
        "night" => null
        "day" => null
      ]
      "standingCharge" => 21.0
      "comparisonRate" => 14.97
    ]
  ]
  "gas" => array:4 [
    "code" => "LDD-G1R-VAR-0002"
    "name" => "Go with the flow 2 - Gas"
    "costs" => array:4 [
      "year" => "408.91"
      "month" => "34.08"
      "week" => "7.86"
      "savings" => 160.0
    ]
    "details" => array:4 [
      "name" => "Go with the flow 2 - Gas"
      "unitRate" => 2.5998
      "standingCharge" => 22.995
      "comparisonRate" => 3.27
    ]
  ]
  "display" => array:6 [
    "code" => "LDD-E1R-VAR-0002_LDD-G1R-VAR-0002"
    "name" => "Go with the flow 2"
    "idPaymentMethod" => "4"
    "textPaymentMethod" => "Direct Debit"
    "costs" => array:4 [
      "year" => "873.03"
      "month" => "72.75"
      "week" => "16.79"
      "savings" => 186.0
    ]
    "podCopy" => array:2 [
      "subHeading" => "Go with the flow"
      "listContent" => """
        <ul>

        \t<li>No contract, no end-date, no hassle.</li>

        \t<li>Your tariff can go up and down, we&rsquo;ll always tell you first.</li>

        \t<li>Prompt personal service from the friendly TOTO team.</li>

        </ul>
        """
    ]
  ]
  "total" => "873.03"
]
"894.71025-LDD-E1R-VAR-0003_LDD-G1R-VAR-0003" => array:4 [
  "electricity" => array:4 [
    "code" => "LDD-E1R-VAR-0003"
    "name" => "TOTO Discount Variable Saver"
    "costs" => array:4 [
      "year" => "485.80"
      "month" => "40.48"
      "week" => "9.34"
      "savings" => 5.0
    ]
    "details" => array:4 [
      "name" => "TOTO Discount Variable Saver"
      "unitRate" => array:3 [
        "standard" => 13.1985
        "night" => null
        "day" => null
      ]
      "standingCharge" => 21.0
      "comparisonRate" => 15.67
    ]
  ]
  "gas" => array:4 [
    "code" => "LDD-G1R-VAR-0003"
    "name" => "TOTO Discount Variable Saver - Gas"
    "costs" => array:4 [
      "year" => "408.91"
      "month" => "34.08"
      "week" => "7.86"
      "savings" => 160.0
    ]
    "details" => array:4 [
      "name" => "TOTO Discount Variable Saver - Gas"
      "unitRate" => 2.5998
      "standingCharge" => 22.995
      "comparisonRate" => 3.27
    ]
  ]
  "display" => array:6 [
    "code" => "LDD-E1R-VAR-0003_LDD-G1R-VAR-0003"
    "name" => "TOTO Discount Variable Saver"
    "idPaymentMethod" => "4"
    "textPaymentMethod" => "Direct Debit"
    "costs" => array:4 [
      "year" => "894.71"
      "month" => "74.56"
      "week" => "17.21"
      "savings" => 165.0
    ]
    "podCopy" => array:2 [
      "subHeading" => "Discount Variable Saver"
      "listContent" => """
        <ul>

        \t<li>Discounted rate for 12 months.</li>

        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>

        \t<li>12 month contract.</li>

        \t<li>Prompt personal service from the friendly TOTO team.</li>

        </ul>
        """
    ]
  ]
  "total" => "894.71"
]
"1034.19225-LDD-E1R-VAR-0004_LDD-G1R-VAR-0004" => array:4 [
  "electricity" => array:4 [
    "code" => "LDD-E1R-VAR-0004"
    "name" => "TOTO Evergreen Saver"
    "costs" => array:4 [
      "year" => "547.32"
      "month" => "45.61"
      "week" => "10.53"
      "savings" => -57.0
    ]
    "details" => array:4 [
      "name" => "TOTO Evergreen Saver"
      "unitRate" => array:3 [
        "standard" => 15.183
        "night" => null
        "day" => null
      ]
      "standingCharge" => 21.0
      "comparisonRate" => 17.66
    ]
  ]
  "gas" => array:4 [
    "code" => "LDD-G1R-VAR-0004"
    "name" => "TOTO Evergreen Saver - Gas"
    "costs" => array:4 [
      "year" => "486.87"
      "month" => "40.57"
      "week" => "9.36"
      "savings" => 82.0
    ]
    "details" => array:4 [
      "name" => "TOTO Evergreen Saver - Gas"
      "unitRate" => 3.2235
      "standingCharge" => 22.995
      "comparisonRate" => 3.89
    ]
  ]
  "display" => array:6 [
    "code" => "LDD-E1R-VAR-0004_LDD-G1R-VAR-0004"
    "name" => "TOTO Evergreen Saver"
    "idPaymentMethod" => "4"
    "textPaymentMethod" => "Direct Debit"
    "costs" => array:4 [
      "year" => "1,034.19"
      "month" => "86.18"
      "week" => "19.89"
      "savings" => 25.0
    ]
    "podCopy" => array:2 [
      "subHeading" => "Evergreen Saver"
      "listContent" => """
        <ul>

        \t<li>No contract, no end date, no hassle.</li>

        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>

        \t<li>Prompt friendly service from the friendly TOTO team.</li>

        </ul>
        """
    ]
  ]
  "total" => "1,034.19"
]

] ]

  • 写回答

1条回答 默认 最新

  • dqhr76378 2017-02-10 17:44
    关注

    I guess You want some kind of this.

    Type in comments if something went wrong (I have not checked my code).

    $unsortedData = [...];
    
    $sortByTotal = function($a, b) {
      $aTotal = (float)str_replace(',', '', $a['total']);  // Your total value is string like "1,034.19" so making it normalized to easily compare
      $bTotal = (float)str_replace(',', '', $b['total']);
    
      return $aTotal <=> $bTotal; // read about "starship operator" in PHP 7+
    };
    
    foreach($unsortedData AS $meter => $records) {
       usort($unsortedData[$meter], $sortByTotal);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化