嗨,我有这个数组进来:
$myRegionData =
array (
[0] => stdClass Object
(
[howmuch] => 1
[country] => ID
[state] => JAWA BARAT
)
[1] => stdClass Object
(
[howmuch] => 1
[country] => RO
[state] => BUCURESTI
)
[2] => stdClass Object
(
[howmuch] => 2
[country] => US
[state] => CALIFORNIA
)
[3] => stdClass Object
(
[howmuch] => 1
[country] => US
[state] => TEXAS
)
)
我试图将数组输出分组为这样
ID
JAWA BARAT (1)
RO
BUCURESTI (1)
US
CALIFORNIA (2)
TEXAS (1)
我尝试了键值关联,i Loops等,我似乎可以在显示中组合美国各州。
任何建议将不胜感激
我会先按国家/地区重新组织它,以使事情变得更容易:
// will hold the re-indexed array
$indexed = array();
// store each state's object under the country identifier
foreach($myRegionData as $object) {
if(!isset($indexed[$object->country])) {
$indexed[$object->country] = array();
}
$indexed[$object->country][] = $object;
}
// output the data
foreach($indexed as $country => $states) {
echo $country, "'n";
foreach($states as $state) {
printf(" %s (%u)'n", $state->state, $state->howmuch);
}
}