2

My result in php returns following array.

Array (
    [137] => governmentuser1 
) 
Array (
    [198] => testadmin
)
Array (
    [215] => adminregion
)
Array (
    [224] => testcompany1234 
) 
Array (
    [225] => testuser12345678 
)

how can i convert it to this form to set option value in cakephp selectbox

Array (
    [137] => governmentuser1
    [198] => testadmin
    [215] => adminregion
    [224] => testcompany1234
    [225] => testuser12345678

)
2
  • Where do you get this from? If you're using Cake, it seems like you're doing something wrong to get this kind of array... Commented Nov 26, 2011 at 11:33
  • jafar asked this in another question. I think the unusual format is due to how that part of jafar's app is coded and should probably be addressed there, instead of combining these results afterward. Commented Nov 26, 2011 at 18:40

2 Answers 2

5

It's as easy as this:

$total = $result1 + $result2 + $result3 + ...

What you call "your result" is actually not one single array - you have 5 different arrays there. To preserve the keys you need to merge them using the + (plus) operator instead of using array_merge.

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

Comments

1

You can use array_merge() function to help you merge all the arrays together.

2 Comments

Beware that If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator
Looks like his keys are the IDs of records in his database so it wouldn't matter if he overwrote elements with matching keys or not (as they would contain the same values).

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.