0

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
    )
)
2
  • have u tried $arr += array('name' => $id); ? Commented Dec 2, 2010 at 16:55
  • The problem was just me being stupid. "Doing $arr[] = array();" is essentially the same as += Commented Dec 3, 2010 at 15:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that's what I was wanting. Not sure how I got confused and thought that I needed them all on the same index. My bad...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.