php - convert array to associative array

837

I have an array like this:

Array
(
    [0] => name
    [1] => john
    [2] => last
    [3] => doe
    [4] => company
    [5] => sony
)

I need to convert to this:

Array
(
    [name] => john
    [last] => doe
    [company] => sony
)

Any ideas?

48

Answer

Solution:

for ($i = 0; $i < count($myArray); $i += 2)
    $newArray[ $myArray[$i] ] = $myArray[$i+1];

People are also looking for solutions to the problem: Handling HTML Form Array with PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.