My array looks like this:
我的数组看起来像这样:
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"
)
)
)
)
What I want to achieve is get an array with the options added on duplicate state names. So that I end up with San Francisco options having 2 arrays in it and so on for any other duplicate state names I get.
我想要实现的是获取一个数组,其中包含在重复状态名称上添加的选项。因此,我最终得到了包含2个数组的旧金山选项,以及其他任何其他重复状态名称。
How do I do this in php?
我如何在PHP中执行此操作?
What I would like to have is:
我想拥有的是:
Combine the arrays so that the "options" part of "state" has the multiples. For example "San Franciso" would have its Options section as 2 arrays with SF1 and SF2. I hope that is clear.
组合数组使“状态”的“选项”部分具有倍数。例如,“San Franciso”将其选项部分作为具有SF1和SF2的2个阵列。我希望这很清楚。
1 个解决方案
#1
1
I think you need something similar:
我认为你需要类似的东西:
$output_array = array();
foreach($input_array as $input){
$output_array[$input['state']][] = $input;
}
演示
#1
1
I think you need something similar:
我认为你需要类似的东西:
$output_array = array();
foreach($input_array as $input){
$output_array[$input['state']][] = $input;
}
演示