tags:

views:

48

answers:

2

Hi!

I have this multidimensional array:

Array ( [0] => Array ( [id] => 1 [list_name] => List_Red ) [1] => Array ( [id] => 2 [list_name] => List_Blue ) )

...and i would like to create a new array containing only the [id]'s from it.

I would appreciate it alot if you guys could help me with that ^^

Thanks in advance

+1  A: 
foreach($array as $label => $data)
{
    $final[] = $data['id'];
}
fabrik
worked like a charm! thanks! :)
pitic
You're welcome :)
fabrik
Please don't forget to accept my answer as correct :)
fabrik
+1  A: 

@fabrik Your solution indeed does work but it is also incorrect as PHP will throw a E_WARNING telling you that you're appending to an array that did not yet exist. Always initialise your variables before you use them.

$newList = array();
foreach($myList as $listItem) {
    $newList[$listItem['id']] = $listItem['list_name'];
}

This is now a list of all your list_names in the following format.

Array (
    1 => List_Red
    2 => List_Blue
)

Much easier for you to work with and you can now iterate over it like so..

foreach($newList as $itemID => $itemName) {
    echo "Item ID: $itemID - Item Name: $itemName<br>";
}
Paul Dragoonis
Thanks. I learned alot from this :)
pitic