Cakephp 3有许多关联数组,可以用适当的json格式进行转换

时间:2018-05-08 17:06:28

标签: php arrays json cakephp-3.0

我有一个如下数组,我需要以json格式排列。我正在使用cakephp3,其中员工表与里程表和费用表有很多关系。我重新检索了需要以适当的json格式排列的完整数组。我尝试但无法转换为正确的json格式。

Array
(
    [0] => App\Model\Entity\Employee Object
        (
            [id] => 246
            [department] => Management
            [employee_name] => Employee_1
            [car_registration] => uk074wzx
            [expenses] => Array
            (
                [0] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 246
                        [expense_amount] => 160
                    )

                [1] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 246
                        [expense_amount] => 150              
                    )
            )

            [mileages] => Array
                (
                    [0] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 246
                        [mileage_km] => 8.918
                        [mileage_amount] => 78.4
                        [subsistence] => domestic_day
                        [subsistence_rate] => 33.61
                    )

                    [1] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 246
                        [mileage_km] => 70.671
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )

                    [2] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 246
                        [mileage_km] => 40.997
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )
            )
    )

    [1] => App\Model\Entity\Employee Object
    (
        [id] => 247
        [department] => Testing
        [employee_name] => Employee_2
        [car_registration] => uk074wzx
        [expenses] => Array
            (
                [0] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 247
                        [expense_amount] => 50
                    )

                [1] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 247
                        [expense_amount] => 30
                    )
            )

        [mileages] => Array
            (
                [0] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 247
                        [mileage_km] => 131.137
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )

                [1] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 247
                        [mileage_km] => 131.018
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )
            )
    )

    [2] => App\Model\Entity\Employee Object
    (
        [id] => 248
        [department] => Design
        [employee_name] => sbn emp3
        [car_registration] => uk074wzx
        [expenses] => Array
            (
            )

        [mileages] => Array
            (
            )
    )
)

我需要安排Json格式:

"data": [
    {
    "date": "2018-04-30(month end date)",
    "items": [
            {
                "employee_id": 246,
                "employee_name": "Employee_1",
                "department": "Management",
                "car_registration": "uk074wzx",
                "mileage_km": 120.586,
                "mileage_amount": 235.2,
                "day_allowance": 100.83,
                "night_allowance": 0
                "expense_amount": 310
            },
            {
                "employee_id": 247,
                "employee_name": "Employee_2",
                "department": "Testing",
                "car_registration": "uk074wzx",
                "mileage_km": 382.741,
                "mileage_amount": 392,
                "day_allowance": 168.05,
                "night_allowance": 0
                "expense_amount": 80
            },
            {
                "employee_id": 248,
                "employee_name": "sbn emp3",
                "department": "Design",
                "car_registration": "uk074wzx",
                "mileage_km": 0,
                "mileage_amount": 0,
                "day_allowance": 0,
                "night_allowance": 0
                "expense_amount": 0
            }
        ]
    }
]

请帮我将数组转换为json。三江源。

1 个答案:

答案 0 :(得分:2)

在EmployeeController类

sed 's/Dipesan pada \\\\r\\\\n//g' file.json # bad
sed 's/Dipesan pada\\\\r\\\\n //g' file.json # good