A
A
Artyom2015-10-06 16:12:17
PHP
Artyom, 2015-10-06 16:12:17

Convert array (php)?

Array
(
[0] => id
[1] => 2
[2] => product
[3] => 3
)
Do Array('id'=>2, 'product'=>3).
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cat Anton, 2015-10-06
@Shumerok

$arr = array('id', 2, 'product', 3);
$new = array();

for ($i = 0; $i < count($arr) - 1; $i += 2) {
    $new[$arr[$i]] = $arr[$i + 1];
}

var_dump($new);

D
Dmitry Novikov, 2015-10-06
@dmitriy_novikov

in the first case, you have the result of the array formation, and in the second, the array formation string.
It's like asking "how to make (2 + 3) out of 5?"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question