I am working with a web service that returns JSON data, which, when I decode with PHP’s json_decode function, gives me an array like the following:
Array
(
[0] => eBook
[1] => 27
[2] => Trade Paperback
[3] => 24
[4] => Hardcover
[5] => 4
)
Is there a PHP function that will take this array and combine it into an associative array where every other element is a key and the following element is its value? What I want to come up with is the following:
Array
(
[eBook] => 27
[Trade Paperback] => 24
[Hardcover] => 4
)
Your own solution is clearer for sure, but this one-liner, made of PHP functions only, does the same job:
Your question was “is there a PHP function…”. The answer is no, but for a combination thereof it is yes.
The tricky part here is the transposition of the chunked array, which is achieved by calling
array_mapwithnullas the first argument (see the manual where it says “an interesting use of this function…”)