S
S
semki0962016-05-24 11:50:23
PHP
semki096, 2016-05-24 11:50:23

How to get key-value string from PHP array?

There is an array

Array
(
    [0] => 31
    [1] => 5
    [2] => 6
    [3] => 4
)

translate into a string
json_encode($a)
I get the result:
[31,5,6,4] The
question is how can I get this result:
{"1":31, "2":5, "3":6, "4":4}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-05-24
@semki096

how can i get this result
Increment all keys by one. If an associative array has a key of 0 , this array is converted to json exactly as an array, not as an object.
For example, you can use the following code:
$a = [31, 5, 6, 4];

for ($index = count($a); $index > 0; $index--) {
  $a[$index] = $a[$index - 1];
  unset($a[$index - 1]);
}

ksort($a);

Of course, it is applicable only for ordinary arrays - indexing from zero without gaps.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question