I have this array:
Array
(
[0] => Array
(
[id] => 70170b60a4
[name] => vip
)
[1] => Array
(
[id] => 355a5f8cbd
[name] => TEST LIST2
)
[2] => Array
(
[id] => d1c2deef1d
[name] => test list
)
)
I call it $listInfo.
Now I want to take the info from this array and create a new one, that looks like this:
Array
(
[70170b60a4] => vip
[355a5f8cbd] => TEST LIST2
[d1c2deef1d] => test list
)
I do this by:
foreach ($listinfo as $key) {
$list = array($key['id'] => $key['name'] );
}
Then I want to return the value (id) of vip:
$listId = array_search('vip', $list);
My problem is that the foreach loop does only create an array with the last values in the first array. so I get:
Array
(
[d1c2deef1d] => test list
)
Can anyone tell me what I am doing wrong? I want all the values from the first array in there. Thanks a lot for any help.
In your foreach
loop, you're creating a new array every time.
Try this:
$list = array();
foreach ($listinfo as $key) {
$list[$key['id']] = $key['name'];
}
That's because you are redefining $list
every loop iteration. what you want to do is:
$list = array();
foreach($listinfo as $a) {
$list[$a['id']] = $a['name'];
}
You're creating a new array each time your loop is executed, try this:
$list = array();//make the array, once and only once
foreach ($listinfo as $key)
{
$list[$key['id']] = $key['name'];//assign new key, new value to EXISTING array
}