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]
   }]
4

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this json_encode function

echo json_encode($your_array);

Comments

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

Comments

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.

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.