我的数组看起来像这样:
array(4) (
0 => array(3) (
"id" => string(1) "2"
"state" => string(8) "San Francisco"
"options" => array(2) (
"value" => string(1) "1"
"label" => string(8) "SF1"
)
)
1 => array(3) (
"id" => string(1) "2"
"state" => string(8) "San Francisco"
"options" => array(2) (
"value" => string(1) "2"
"label" => string(6) "SF2"
)
)
2 => array(3) (
"id" => string(1) "1"
"state" => string(7) "New Jersey"
"options" => array(2) (
"value" => string(1) "3"
"label" => string(9) "NJ1"
)
)
3 => array(3) (
"id" => string(1) "1"
"state" => string(7) "New Jersey"
"options" => array(2) (
"value" => string(1) "4"
"label" => string(10) "NJ2"
)
)
)
我想要实现的是获取一个在重复状态名称上添加选项的数组。所以我最终得到了旧金山的选项,其中包含2个数组,以此类推,因为我得到的任何其他重复的州名。
我如何在php中执行此操作?
我想要的是:
组合数组以便"选项" "州"的一部分有倍数。例如" San Franciso"将其选项部分作为具有SF1和SF2的2个阵列。我希望这很清楚。
答案 0 :(得分:1)
我认为你需要类似的东西:
$output_array = array();
foreach($input_array as $input){
$output_array[$input['state']][] = $input;
}