嗨,我有这个阵列进来了:
$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)
我尝试了Key值关联,我循环等等。我似乎可以在显示中结合美国各州。
非常感谢任何建议
答案 0 :(得分:1)
我首先按国家/地区对其进行重新组织以简化操作:
// 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);
}
}