I'm trying to create an associative array with dynamic data, and having some trouble.
I'd like to produce an array that looks like the following while fetching rows from a MySQL query.
Array
(
[0] = Array
(
[name] => First
)
[1] = Array
(
[name] => Second
)
[2] = Array
(
[name] => Third
)
[3] = Array
(
[name] => Fourth
)
[4] = Array
(
[name] => Fifth
)
)
I've been trying to use array_merge, but it's not giving me the result I want. Array_merge apparently doesn't operate the same inside a foreach as it does outside (I ran the same code with and without the loop, without worked the way I need).
Basically, this is what I'm doing currently (which doesn't work):
foreach($idList as $id)
{
$arr[] = array_merge(array(), array('name' => $id));
}
This gives me output like this:
Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[0] = Array
(
[name] => first
)
[name] => second
)
[2] = Array
(
[0] = Array
(
[name] => first
)
[1] = Array
(
[0] = Array
(
[name] => first
)
[name] => second
)
[name] => third
)
)
$arr += array('name' => $id);?