tags:

views:

115

answers:

2

I have this array:

Array
(
    [0] => Array
        (
            [tag_id] => 1
        )

    [2] => Array
        (
            [tag_id] => 3
        )

    [22] => Array
        (
            [tag_id] => 44
        )

    [23] => Array
        (
            [tag_id] => 45
        )

    [25] => Array
        (
            [tag_id] => 47
        )

    [26] => Array
        (
            [tag_id] => 48
        )

)

I'd like it to look something like this so its simpler for me to loop through and insert each value into a database:

Array
(
    [0] => 1
    [1] => 3
    [2] => 44
    [3] => 45
    [4] => 47
    [5] => 48
)
+3  A: 

Well, you could do this:

$new_array = array();
foreach($array as $key => $value)
{
    $new_array[$key] = $value['tag_id'];
}
print_r($new_array);
Chacha102
related. http://davidwalsh.name/flatten-nested-arrays-php
+7  A: 

You can use array_map.

PHP 5.3 or higher:

$callback = function($value) {
   return $value['tag_id'];
};
$result = array_map($callback, $array);

Below 5.3:

function collapseTagIds($value) {
  return $value['tag_id'];
}
$result = array_map('collapseTagIds', $array);
Mads Jensen
Both of your answes came in handy, I was stumped with why this wasn't working when I moved to a live server. Checked back here and you saved me yet again (PHP versions were different), so just wanted to say thanks!
Danny
You're very welcome :)
Mads Jensen