1

Below is my php array,

Array
(
    [0] => Array
        (
            [requests] => 22
        )

    [1] => Array
        (
            [requests] => 44
        )
    [overview_chart_data] => Array
        (
            [response] => ok
        )
)

below is php json encoded result of above array ,

{"0":{"requests":22},"1":{"requests":44},"overview_chart_data":{"response":"ok"}},

for some reason I need to get the result like below,

{{"requests":22},{"requests":44},"overview_chart_data":{"response":"ok"}}

Is it possible to get by expected result..! Anyhelp Appreciated...

Thiyagu
  • 746
  • 3
  • 11
  • 29

3 Answers3

2

You can use a custom script for that:

<?php
$a = [
    ['requests' => 22],
    ['requests' => 44],
    'overview_chart_data' => ['response' => 'ok']
];
$output = [];
foreach ($a as $k => $v)
    $output[] = (is_string($k) ? ('"' . $k . '":') : '') . json_encode($v);

echo '{' . implode(',', $output) . '}' . PHP_EOL;

But it is not a valid JSON document.

Neodan
  • 5,154
  • 2
  • 27
  • 38
1

Here is a good S.O. post to help you get these results by doing this...

$testArray = array(
    (object)array("name" => "John", "hobby" => "hiking"),
    (object)array("name" => "Jane", "hobby" => "dancing")
);

echo "Person 1 Name: ".$testArray[0]->name;
echo "Person 2 Hobby: ".$testArray[1]->hobby;

This will give you the desired outcome of having objects in an array without numerical indices... however a much more proper way would be to follow this post instead. This way you can use the correct JSON decoding procedures to capture the ability to do this kind of easy syntax $obj = json_decode($data);.

GoreDefex
  • 1,461
  • 2
  • 17
  • 41
1

try using $array = array_values($array); That will achieve what you are looking for without too much hassle

ln -s
  • 325
  • 3
  • 12