我有这个数组。我想把这个数组中只有一个的内容提取到一个新的数组中([选项])。我如何在php中做到这一点?
Array
(
[0] => Array
(
[key] => field_54df3275b708a
[label] => Idioma
[name] => idioma
[type] => select
[instructions] =>
[required] => 0
[choices] => Array
(
[Catalan] => Catalan
[Castellano] => Castellano
[Ingles] => Ingles
)
[default_value] =>
[allow_null] => 0
[multiple] => 0
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => null
[operator] => ==
[value] =>
)
)
[allorany] => all
)
[order_no] => 0
)
)
我尝试这个代码(我认为这是一个坏代码):
foreach($array as $k=>$v){
if (is_array($v)){
foreach($v as $l=>$w){
if ($w){
foreach($w as $s=>$t){
$idiomas[]=$t.'<br />';
}
}
}
}
}
但它将[选项]和[条件_逻辑]保存到新数组中,我只想要[选项]
非常感谢
$new_array_choices = $array[0]['choices'];
我花了一段时间创建了一个测试代码小提琴。我不得不手动重新创建数组。
由于您一直在每个foreach中获取密钥,因此可以使用它来确保它是您想要的数组:
foreach($array as $k=>$v) {
if (is_array($v)) {
foreach($v as $l=>$w) {
if ($w && $l == 'choices') { // $w is the wanted array
foreach($w as $s=>$t) {
$idiomas[]=$t.'<br />';
}
}
}
}
}
我不确定如果($w)
通过
用实际的数组替换$arr
,它仍然可以工作。
// your array (replace)
$arr = array(
"required" => 0,
"choices" => array(
"Catalan" => "Catalan",
"Castellano" => "Castellano",
"Ingles" => "Ingles",
),
"default" => NULL,
);
// the empty resulting array
$new_arr = array();
foreach($arr["choices"] as $key)
array_push($new_arr, $arr["choices"][$key]);
// your resulting array
print_r($new_arr);
所需的数组保存在数组$new_arr
中。