I have this JSON array:
[
{
"id": 101,
"name": "White"
},
{
"id": 102,
"name": "Black"
}
]
It was generated by my **actionIndex()** function and Yii2 Framework:
public function actionIndex()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$colors = Colors::find()
->select([
'id',
'name'
])
->asArray()
->all();
return $colors;
}
I need to add some text to the array. The result has to be like this:
{
"error": false,
"error_message": null,
"data": [
{
"id": 101,
"name": "White"
},
{
"id": 102,
"name": "Black"
}
]
}
I tried to use array_push function but it doesn't work:
$father = [];
array_push($father, $colors);
error,error_messageand thendatawhich contains your array