1

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.

2
  • 1
    Why don't you alter your query rather than processing the result with PHP? Commented Mar 25, 2011 at 15:03
  • I don't know how to alter my query... here it is - SHOW COLUMNS FROM root_blocks. thanks. Commented Mar 25, 2011 at 15:16

4 Answers 4

5

I guess you could use

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

} 
Sign up to request clarification or add additional context in comments.

Comments

2

When you only need the first entry from each subarray:

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

Comments

1
$fields = array();

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

print_r($fields);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.