开发者

PHP: how to make a nested array into one dimensional array?

开发者 https://www.devze.com 2023-02-19 14:07 出处:网络
This is a nested array I got from my db, Array ( [0] => Array ( [Field] => blc_id [Type] => int(10) unsigned

This is a nested array I got from my db,

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
        )

)

How can I use foreach loop to get the result below,

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

Here is my code so far but of course it doesnt return the result correctly,

开发者_开发知识库
foreach($items as $outer_key => $array)
{ 
    foreach($array as $inner_key => $value)
    { 
        $field_name[] = $value; 
    } 
} 

Thanks.


I guess you could use

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

} 


When you only need the first entry from each subarray:

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


$fields = array();

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

print_r($fields);


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号