Jah Jah - 5 months ago 33
PHP Question

php join multiple json files and group them by id

I have multiple json files, i need to join them all to gather to create my dynamic menu.

this is the current output

{
"Dashboard": {
"parent": "Dashboard",
"url": "#\/dashboard",
"submenu": []
},
"HRM": {
"submenu": [
{
"title": "Holiday",
"url": "#\/holidays"
},
{
"divider": "true"
},
{
"header": "Header 2"
},
{
"title": "Again...a link.",
"url": "errrr"
}
]
} }


this is the desired output

{
"parent": "Dashboard",
"url": "#\/dashboard",
"submenu": []
}, {
"parent": "HRM",
"submenu": [{
"title": "Holiday",
"url": "#\/holidays"
}, {
"divider": "true"
}, {
"header": "Header 2"
}, {
"title": "Again...a link.",
"url": "errrr"
}, {
"header": "Holiday"
},
{
"title": "list",
"url": ""
},
{
"title": "test",
"url": ""
}]
}


here is my PHP code

$files = \File::allFiles($directory);
foreach ($files as $file)
{
if (preg_match("/config.json/i", $file) && \File::size($file) >0) {
$menu =json_decode(\File::get($file), TRUE)['menu'];
// $menu = $menu['menu'];

$id = $menu['parent'];
if(!empty($menu['submenu'])){
$result[$id]['submenu'] = $menu['submenu'];
}else{
$result[$id] = $menu;
}
}
}


JSON files 1 example

{
"menu": {
"parent": "Dashboard",
"url": "#/dashboard",

"submenu": [
]
}

}


JSON file 2 example

{
"menu": {
"parent": "HRM",
"submenu": [
{
"title": "Holiday",
"url": "#/holidays"
},
{
"divider": "true"
},
{
"header": "Header 2"
},
{
"title": "Again...a link.",
"url": "errrr"
}
]
}

}


JSON file 3 example

{
"menu": {
"parent": "HRM",
"submenu": [
{
"header": "Holiday"
},
{
"title": "list",
"url": ""
},
{
"title": "test",
"url": ""
}
]
}

}

Answer

I have made some changes to your code,

$files = \File::allFiles($directory);
$result = array();

foreach ($files as $file)
{
    if (preg_match("/config.json/i", $file) && \File::size($file) >0) {
    $parent = $menu['parent'];
    //checker is used to see if document is already added under an existing parent id
    $checker = true;

    foreach($result as $key=>$value){
        if(is_array($value) && $value["parent"]==$parent){
            $checker = false;
            //merging the submenus of equal parent ids
            $submenuSet = array();
            if(!empty($value["submenu"]) && is_array($value["submenu"])){
                foreach($value["submenu"] as $value1){
                    $submenuSet[] = $value1;
                }
            }
            if(!empty($menu["submenu"]) && is_array($menu["submenu"])){
                foreach($menu["submenu"] as $value2){
                    $submenuSet[] = $value2;
                }
            }
            $value["submenu"] = $submenuSet;
        }
        $result[$key] = $value;
    }

    //if a new parent id add it to results
    if($checker){
        $result[] = $menu;
    }
}
}

the output would be like this,

[
  {
    "parent": "Dashboard",
    "url": "#\/dashboard",
    "submenu": [

    ]
  },
  {
    "parent": "HRM",
    "submenu": [
      {
        "title": "Holiday",
        "url": "#\/holidays"
      },
      {
        "divider": "true"
      },
      {
        "header": "Header 2"
      },
      {
        "title": "Again...a link.",
        "url": "errrr"
      },
      {
        "header": "Holiday"
      },
      {
        "title": "list",
        "url": ""
      },
      {
        "title": "test",
        "url": ""
      }
    ]
  }
]
Comments