dongniaoli1822 2018-09-27 13:27
浏览 38
已采纳

如何将Hierarchical从数组转换为单个数组

I have a data in below JSON format.

$strTree = '{"id":"1","children":[{"id":"316","children":[{"id":"336","children":[{"id":"423"}]},{"id":"337","children":[{"id":"418"}]},{"id":"420"}]},{"id":"405"},{"id":"421"}]}';

And Now I have to build new array using this data for identifying reporting manager

$strTree = [
    '316' => '1',
    '405' => '1',
    '421' => '1',
    '336' => '316',
    '337' => '316',
    '420' => '316',
    '418' => '337',
    '423' => '336',
]

Here What I have tried, but didn't find out the solution to get the expected result

$strTree = '{
    "id": "1",
    "children": [{
        "id": "316",
        "children": [{
                "id": "336",
                "children": [{"id": "423"}]
            },
            {
                "id": "337",
                "children": [{"id": "418"}]
            }, {"id": "420"}
        ]
    },
    {"id": "405"},
    {"id": "421"}
]}';
$arr = (array) json_decode($strTree);
$arrHierarchicalEmpDetails = buildResultedArray($arr, 1);

function buildResultedArray( $elements, $parentId = 0) {
    $branch = [];
    $elements = (array) $elements; 
    foreach ($elements as $element) {
        $element = (array) $element;
        $intID = $element['id'];
        $branch[ $intID ] = $parentId;
        if (!empty( $element['children'])) {
            buildTree( $element['children'], $element['id']);
        }
    }
    return $branch;
}
echo '<pre>'; print_r($arrHierarchicalEmpDetails);
  • 写回答

1条回答 默认 最新

  • douren1891 2018-09-27 14:44
    关注

    Gerber, to solve this problem, you could use a recursive function to "flatten" the hierarchical array:

    function flattenHierarchicalArray($inputArray, $parentId = null)
    {
        $flattenedData = [];
        if (!empty($inputArray['children'])) {
            foreach ($inputArray['children'] as $child) {
                $flattenedData += flattenHierarchicalArray($child, $inputArray['id']);
            }
        }
    
        if (!is_null($parentId)) {
            $flattenedData[$inputArray['id']] = $parentId;
        }
    
        return $flattenedData;
    }
    

    You should call the function on this way:

    flattenHierarchicalArray($data)
    

    Where $data is the hierarchical array decoded from your JSON example. Output:

    array(8) {
      [423]=>
      string(3) "336"
      [336]=>
      string(3) "316"
      [418]=>
      string(3) "337"
      [337]=>
      string(3) "316"
      [420]=>
      string(3) "316"
      [316]=>
      string(1) "1"
      [405]=>
      string(1) "1"
      [421]=>
      string(1) "1"
    }
    

    Note: this function does not keep the order of your expected output, I've asumed that wasn't important at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮