PHP:如何将嵌套数组转换为一维数组?

时间:2011-03-25 15:00:07

标签: php loops multidimensional-array foreach

这是我从我的数据库中获得的嵌套数组,

Array
(
    [0] => Array
        (
            [Field] => blc_id
            [Type] => int(10) unsigned
            [Null] => NO
            [Key] => PRI
            [Default] => 
            [Extra] => auto_increment
        )

    [1] => Array
        (
            [Field] => blc_email
            [Type] => varchar(255)
            [Null] => YES
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [2] => Array
        (
            [Field] => cat_id
            [Type] => varchar(255)
            [Null] => YES
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [3] => Array
        (
            [Field] => blc_created
            [Type] => timestamp
            [Null] => NO
            [Key] => 
            [Default] => 0000-00-00 00:00:00
            [Extra] => 
        )

    [4] => Array
        (
            [Field] => blc_updated
            [Type] => timestamp
            [Null] => NO
            [Key] => 
            [Default] => CURRENT_TIMESTAMP
            [Extra] => on update CURRENT_TIMESTAMP
        )

)

如何使用foreach循环获取下面的结果,

Array
(
    [0] => blc_id
    [1] => blc_email
    [2] => cat_id
    [3] => blc_created
    [4] => blc_updated
)

到目前为止,这是我的代码,但当然它没有正确返回结果,

foreach($items as $outer_key => $array)
{ 
    foreach($array as $inner_key => $value)
    { 
        $field_name[] = $value; 
    } 
} 

感谢。

4 个答案:

答案 0 :(得分:5)

我想你可以使用

foreach($items as $outer_key => $array)
{ 
    $field_name[] = $array['Field']; 

} 

答案 1 :(得分:2)

当您只需要每个子阵列的第一个条目时:

$field_names = array_map("current", $outer_key);

答案 2 :(得分:1)

$fields = array();

foreach($input_array as $value)
  $fields[] = $value['Field'];

print_r($fields);

答案 3 :(得分:0)

array_map(create_function('$a','return $a["Field"];'),$a)