PHP: how to make a nested array into one dimensional array?
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)
精彩评论