按子子数组值排序数组

时间:2017-02-10 17:21:18

标签: php arrays sorting

如何对以下数组进行排序。

顶级阵列仪表类型:

“使用智能电表”, “用老式仪表

在这些内部有各种关税,也是阵列。但每个关税都有。我需要将每个米类型数组内的资费按数组中最低的第一个进行排序。

我如何记住这一点我每次生成数组时都不知道关税名称。例如,tarriff名称873.03195-SDD-E1R-VAR-0002_SDD-G1R-VAR-0002每次都可能不同。

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>\r\n
        \t<li><span style="font-family: Arial, sans-serif; font-size: 14px;">Say goodbye to estimated billing with a TOTO smart meter</span></li>\r\n
        \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>\r\n
        </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>\r\n
        \t<li>No contract, no end date, no hassle.</li>\r\n
        \t<li>Say goodbye to estimated billing with a TOTO Smart meter and FREE in home display.</li>\r\n
        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n
        \t<li>Prompt friendly service from the friendly TOTO team.<br />\r\n
        \t</li>\r\n
        </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>\r\n
        \t<li>Discounted rate for 12 months.</li>\r\n
        \t<li>Say goodbye to estimated billing with a TOTO Smart meter and FREE in home display.</li>\r\n
        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n
        \t<li>12 month contract.</li>\r\n
        </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>\r\n
        \t<li>No contract, no end-date, no hassle.</li>\r\n
        \t<li>Your tariff can go up and down, we&rsquo;ll always tell you first.</li>\r\n
        \t<li>Prompt personal service from the friendly TOTO team.</li>\r\n
        </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>\r\n
        \t<li>Discounted rate for 12 months.</li>\r\n
        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n
        \t<li>12 month contract.</li>\r\n
        \t<li>Prompt personal service from the friendly TOTO team.</li>\r\n
        </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>\r\n
        \t<li>No contract, no end date, no hassle.</li>\r\n
        \t<li>Your tariff can go up or down, we&rsquo;ll always tell you first.</li>\r\n
        \t<li>Prompt friendly service from the friendly TOTO team.</li>\r\n
        </ul>
        """
    ]
  ]
  "total" => "1,034.19"
]

] ]

1 个答案:

答案 0 :(得分:1)

我想你想要某种这样的东西。

如果出现问题,请输入评论(我没有检查过我的代码)。

$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);
}
相关问题