0

I want to create an array which response come like this

response should like this

   [{
        name: 'Installation',
        data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
    }, {
        name: 'Manufacturing',
        data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
    }, {
        name: 'Sales & Distribution',
        data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
    }, {
        name: 'Project Development',
        data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
    }, {
        name: 'Other',
        data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
   }]
Ravi Parmar
  • 91
  • 11

4 Answers4

1

Try below code, I just temporary set same data in for loop.

$obj = new \stdClass;
$all = array();
for($i = 0; $i < 2; $i++) {
    $obj->name= "test$i";
    $obj->data = [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175];
    $all[] =  $obj;
}
echo json_encode($all);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Madhusudan
  • 401
  • 3
  • 12
0

Try this json_encode function

echo json_encode($your_array);
Siddharth Ramani
  • 661
  • 3
  • 12
0

create an array in php then use json_encode function. it will convert the array to json format. Similarly, json_decode function will convert the json values to array or object.

here you will get the relavent help -

http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/function.json-decode.php

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Sarwar
  • 59
  • 8
0
    foreach ($sNames as $key => $name) {
        if (!empty($name)) {
            $services[$key] = array(
                'name' => $name,
                'data' => $sData[$key],
            );
        }
    }
    $data['services'] = json_encode($services);

i think you have to do something like this, may be this will help you.