简体   繁体   English

将 PHP 数据转换为 JSON 文件

[英]Convert PHP datas to a JSON file

i need help我需要帮助

I have data that comes from a database and gets in a table and with this data I would like to create a json file, here is the structure of my table:我有来自数据库并进入表的数据,我想用这些数据创建一个 json 文件,这是我的表的结构:

Array
(
    [TSHIMAMBU KAFIMBA JEAN] => Array
        (
            [0] => Array
                (
                    [TAMBWE MANANGA JOSEPH] => Array
                        (
                            [0] => Array
                                (
                                    [MBALA SHABANI JEAN PAUL] => 
                                )

                            [1] => Array
                                (
                                    [MUSANGU KYULU BENOIT] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [MBUYI MUKADI ROGER] => 
                                                )

                                        )

                                )

                            [2] => Array
                                (
                                    [MUTOMBO KOLOMONI FRANCOIS] => 
                                )

                            [3] => Array
                                (
                                    [SUNGUNRA MATUNDA ELISÉE] => 
                                )

                        )

                )

            [1] => Array
                (
                    [NDAY WA NDAY DAVID] => Array
                        (
                            [0] => Array
                                (
                                    [LUBINDA BANZA JACQUIE] => 
                                )

                        )

                )

        )

)

and here is the code that helps me get this table这是帮助我获取此表的代码

function myfunction($parent_id, $departement_id){

    $tab = fetchchildren($parent_id, $departement_id);
    $departement_database = new DepartementDatabase();

    $employees  = $departement_database->show_employes_of_departement($departement_id);
    $name_parent = " ";

    foreach ($employees as $employee){
        if($employee['id'] == $parent_id){
            $name_parent = utf8_encode($employee['name_employee']);
            break;
        }
    }
    $tab2 = array();

    foreach ($tab as $value){
        $tab2[$name_parent][] = myfunction($value['id'], $departement_id);
    }
    if (count($tab) == 0){
        $tab2[$name_parent] = null;
    }

    return $tab2;
}
$tab = myfunction(1, 1);

how to browse my array to get this json format????如何浏览我的阵列以获取此 json 格式???? here is the json format i want to have这是我想要的 json 格式

[{
    "label": "President",
    "name": "John Doe",
    "children": [{
        "name" : "Jane Smith",
        "label": "Vice President of Administration",
        "children": [{
            "name": "Peter West",
            "label": "Director of Finance"
        }, {
            "name" : "Sarah Jones",
            "label": "Director of Human Resources"
        }]
    }, {
        "name" : "Richard Easton",
        "label": "Vice President of Operations",
        "children": [{
            "name" : "Amy Thomas",
            "label": "Director of Distribution"
        }, {
            "name" : "Greg Li",
            "label": "Director of Customer Service",
            "children": [{
                "name" : "Laronda Phillips",
                "label": "Technical Support Manager"
            }]
        }]
    }, {
        "name" : "Alice Ozaltin",
        "label": "Vice President of Merchandising",
        "children": [{
            "name": "Zach Kwon",
            "label": "Director of Purchasing",
            "children": [
                {
                    "name": "Jonathan Branham",
                    "label": "Internal Purchasing Manager"}]
        }, {
            "name": "Elizabeth Norman",
            "label": "Director of Appliances"
        }, {
            "name" : "Peter Stevens",
            "label": "Director of Clothing",
            "children": [{
                "name": "Rebecca Hammond",
                "label": "Womens Clothing Planner"
            }, {
                "name": "Alex Kaplan",
                "label": "Mens Clothing Planner"
            }]
        }, {
            "name" : "Mark Hughes",
            "label": "Product Information Coordinator"
        }, {
            "name" : "Elvis Presley",
            "label": "Producteur Musical"
        }]
    }, {
        "name" : "Diego KAO",
        "label": "Vice President of Merchandising",
        "children": [{
            "name": "Zach Kwon",
            "label": "Director of Purchasing",
            "children": [
                {
                    "name": "Jonathan Branham",
                    "label": "Internal Purchasing Manager"}]
        }, {
            "name": "Elizabeth Norman",
            "label": "Director of Appliances"
        }, {
            "name" : "Peter Stevens",
            "label": "Director of Clothing",
            "children": [{
                "name": "Rebecca Hammond",
                "label": "Womens Clothing Planner"
            }, {
                "name": "Alex Kaplan",
                "label": "Mens Clothing Planner"
            }]
        }, {
            "name" : "Mark Hughes",
            "label": "Product Information Coordinator"
        }, {
            "name" : "Elvis Presley",
            "label": "Producteur Musical"
        }]
    }]
}]

Need for help please, thanks需要帮助,谢谢

i found out how to do it with nested loops.我发现了如何使用嵌套循环来做到这一点。 thank you for your attention感谢您的关注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM