0

How can I turn an array like this:

Array ( [1396076400000] => 1587 [1396162800000] => 1776 [1396249200000] => 2860 )

into a JSON array with this format:

[[1396076400000,1587],[1396162800000,1776],[1396249200000,2860]]

I've tried json_encode() but it seems I need to do some work on the array structure before I call that. How can I convert the keys and values of one array into the format that I need?

1
  • 1
    foreach array as key => value items = [value, key], json_encode items. Commented Apr 16, 2014 at 19:06

1 Answer 1

2
function json_custom_encode($arr)
{
    foreach ($arr as $key => $value) {
        $newArr[] = array($key, $value);
    }
    return json_encode($newArr);
}

Try this instead of json_encode.

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

Comments

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.