1

this is my multidimensional array..i know how to encode array to json but not getting actual json expected json result

array (
  1 => 
  array (
    'text' => 'Dashboard',
    'spriteCssClass' => 'rootfolder',
    'expanded' => 'true',
    'id' => '1',
    'item_name' => 'Dashboard',
    'menu_type' => 'item',
    'parent' => '0',
    'items' => 
    array (
      9 => 
      array (
        'text' => 'Application',
        'spriteCssClass' => 'html',
        'id' => '9',
        'item_name' => 'Application',
        'menu_type' => 'header',
        'parent' => '1',
        'items' => 
        array (
        ),
      ),
    ),
  ),
)

after encoding it into json i am getting the following result for encodin i used json_encode($array);

{
    "1": {
        "text": "Dashboard",
        "spriteCssClass": "rootfolder",
        "expanded": "true",
        "id": "1",
        "item_name": "Dashboard",
        "menu_type": "item",
        "parent": "0",
        "items": {
            "9": {
                "text": "Application",
                "spriteCssClass": "html",
                "id": "9",
                "item_name": "Application",
                "menu_type": "header",
                "parent": "1",
                "items": {}
            }
            }}}

i want the following encoded json

{
                "text": "Dashboard",
        "spriteCssClass": "rootfolder",
        "expanded": "true",
        "id": "1",
        "item_name": "Dashboard",
        "menu_type": "item",
        "parent": "0",
        "items": [
             {
                "text": "Application",
                "spriteCssClass": "html",
                "id": "9",
                "item_name": "Application",
                "menu_type": "header",
                "parent": "1",
                "items": {}
             }]
            }

i tried almost everything but not getting my expected json result i want remove the array indexing from json like "1" { and also want to add "[" this after every items: column

3 Answers 3

1

It looks like you just want to json_encode($yourData[1]) instead of just json_encode($yourData)...

Sign up to request clarification or add additional context in comments.

1 Comment

i know how to encode it but i want my formatted json which i am not getting..please see my edited post
1

Your array is not 0 indexed, therefore json_encode assumes its an assoc array.

If you 0 index your array, you should get the expected result, or maybe even remove the index assignment completelely:

array (
  array (
    'text' => 'Dashboard',
    'spriteCssClass' => 'rootfolder',
    'expanded' => 'true',
    'id' => '1',
    'item_name' => 'Dashboard',
    'menu_type' => 'item',
    'parent' => '0',
    'items' => 
    array (
      9 => 
      array (
        'text' => 'Application',
        'spriteCssClass' => 'html',
        'id' => '9',
        'item_name' => 'Application',
        'menu_type' => 'header',
        'parent' => '1',
        'items' => 
        array (
        ),
      ),
    ),
  ),
)

EDIT***

to remove all numerical indexes / convert all "non-assoc" to normal use:

function normaliseArray($arr,$recurse=True) {
    if (!is_array($arr))
       return $arr;

    if (count(array_filter(array_keys($arr), 'is_numeric')) == count($arr))
      $arr = array_values($arr);

    if ($recurse) {
      foreach($arr as $k => $a) {
        $arr[$k] = normaliseArray($a,$recurse);
      }
    }

    return $arr;
  }


json_encode(normaliseArray($array));

try that.

6 Comments

how to remove the array indexing
like above.. dont use the "1 =>" part in your top level array. Or use json_encode(array_values($array));
after using arrays_values function doesnt remove all the indexing it only removes 1st element index
You only have 1 element in your top level array.
@Sachin See most recent edit to normalise all numerically indexed arrays... not tested so might have to make some ammends, but its an idea.
|
0

json_encode will encode it as is. The best you can do is force the array to start at 0 which would be the same as []:

$array = array_values($array);
$array[0]['items'] = array_values($array[0]['items']);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.