这就是我的数组正在寻找
array (size=5)
0 =>
array (size=2)
'key' => string 'PresentAddressDisrict' (length=21)
'value' => string '1' (length=1)
1 =>
array (size=2)
'key' => string 'ParmanentAddressDistrict' (length=24)
'value' => string '2' (length=1)
2 =>
array (size=2)
'key' => string 'Board' (length=5)
'value' => string '1' (length=1)
3 =>
array (size=2)
'key' => string 'FieldChoice' (length=11)
'value' => string '1' (length=1)
4 =>
array (size=2)
'key' => string 'Gender' (length=6)
'value' => string '1' (length=1)
尝试了多种解决方案,例如:
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
echo $v, " ";
}
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
但没有得到理想的结果,仍然找到了方法。
需要是:
array('PresentAddressDisrict' => 1, 'ParmanentAddressDistrict' => 2, 'Board' => 1, 'FieldChoice' => 1, 'Gender' => 1)
请帮帮我。
答案 0 :(得分:2)
使用array_map或其他函数可能有一些光滑的方式,但我会简单地接近这个:
// create a new empty array to store values in
$insert = array();
// iterate over your array of arrays
foreach($my_array AS $values) {
// at this point, $values is something like:
// array('key' => 'Gender', 'value' => '1')
$insert[$values['key']] = $values['value'];
}
var_dump($insert);
答案 1 :(得分:0)
提供cale_b提到的数组函数方法。
$values = array_map( function( $item ) {
return $item['value'];
}, $array );
$keys = array_map( function( $item ) {
return $item['key'];
}, $array );
$array = array_combine( $keys, $values );