Manually creating an associative array

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
    )
)

Answers


You've got a few issues here.

Mainly, you can't have the same index twice. 'name' can be the index once and only once, so you're 'desired' output is impossible.

Also, this statement is pretty problematic

foreach($idList as $id)
{
    $arr[] = array_merge(array(), array('name' => $id));
}

The use of $arr[] = $x is like a push. It adds a new element to the back of the array, numerically indexed.

Your use of array_merge is unnecessary. array_merge returns the second argument merged over the first argument. You are just trying to add a single new element. Also, is that exactly the line you used or did you use array_merge($arr, array('name' => $id)); ???

Try:

foreach($idList as $id)
{
    $arr[] = array('name' => $id);
}

And you will get:

Array
(
    [0] = Array
    (
        [name] => first
    )
    [1] = Array
    (
        [name] => second
    }
 ....

And so on. I'm not sure if this is exactly what you want, but what you proposed in the first place isn't possible.


Need Your Help

How to create a personal wiki+blog on github using org-mode?

github blogs wiki org-mode mathjax

I Love org-mode! I tried to create my personal pages using org-export-html. Org-mode can export latex-math using mathjax very well, and many many other feathers. I love that! I want a tidy and beau...

Linq with entity Framework using a function to select columns

c# sql-server linq entity-framework

I'm trying to reduce the amount of code in my application.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.