Problem with passing values from php to JavaScript array

I would like to ask the expert that the format of the front-end JavaScript array is: var menu= [{id: "11", text: "menu 1", children: [{id: "11", text: "sub-menu 1"}, {id: "112", text: "sub-menu 2"}]}, and how to use php to transfer values in the background?

Php
Mar.15,2021

the front and back end values are transmitted in json format
the original array format should be

array (
  0 => 
  array (
    'id' => '11',
    'text' => '1',
    'children' => 
    array (
      0 => 
      array (
        'id' => '111',
        'text' => '1',
      ),
      1 => 
      array (
        'id' => '112',
        'text' => '2',
      ),
    ),
  ),
)

format it with json_encode and return it
the data returned should be

[{
    "id": "11",
    "text": "1",
    "children": [{
        "id": "111",
        "text": "1"
    }, {
        "id": "112",
        "text": "2"
    }]
}]

convert json_encode to json string

Menu