douwen0647 2017-07-19 08:24
浏览 65
已采纳

如何在PHP中创建JavaScript数组

I need to build an array-within-objects-within-array-within-objects for JavaScript uses. My data is in PHP, and I could organize my necessary information using a foreach loop in an array:

$officeWiseEmployees = array();
foreach($employeeList as $employee) :
    $officeWiseEmployees[$employee->office_name][$employee->employee_id] = $employee->employee_name;
endforeach;

My intention was to make arrays, then I'll typecast with (object) where necessary to make the objects within. By the way the resulted data is:

array:2 [
  "Office 1" => array:2 [
    1 => "User 1"
    2 => "User 2"
  ]
  "Office 2" => array:2 [
    3 => "User 3"
    4 => "User 4"
  ]
]

The desired array-within-objects...:

[{
    text: 'Office Name 1',
    children:
            [ { value: 1, text: 'Employee 1' }, { value: 2, text: 'Employee 2' } ]
},
{
    text: 'Office Name 2',
    children:
            [ { value: 3, text: 'Employee 3' }, { value: 4, text: 'Employee 4' } ]
}]

Issue is, whenever I wanted to proceed to assign the Array Indexes, I's failing within the foreach loops.

$make_up_array = array();
foreach( $officeWiseEmployees as $office_name => $office_employees ) {
    $make_up_array['text'] = $office_name;
    foreach( $office_employees as $employee_id => $employee_name ) {
        // dump($employee_id);
        // dump($employee_name);

    }
}

You can see, on line 3 I's doing wrong, so I's getting only the last Office name under text index. If I introduce another index over text or children, then I's getting further beyond the setup I needed.

How can I convert my PHP array into a JavaScript array-within-objects-within-array-within-objects in an easy way? I thought it could be easier than I might think of, that's why developer used this.

  • 写回答

3条回答 默认 最新

  • dou9022 2017-07-19 08:43
    关注

    As others have mentioned, json_encode will work out whether something is an array or object.

    A lot of the complexity comes in the way you set the user id to the key of each element.

    <?php
    
    $employees = [
        ['id' => 1, 'name' => 'Employee 1', 'office' => 'Office 1'],
        ['id' => 2, 'name' => 'Employee 2', 'office' => 'Office 1'],
        ['id' => 3, 'name' => 'Employee 3', 'office' => 'Office 2'],
        ['id' => 4, 'name' => 'Employee 4', 'office' => 'Office 1'],
        ['id' => 5, 'name' => 'Employee 5', 'office' => 'Office 2'],
    ];
    
    $offices = [];
    foreach ($employees as $employee) {
        $offices[$employee['office']][] = [$employee['id'] => $employee['name']];
    }
    
    $officesOutput = [];
    foreach ($offices as $name => $officeEmployees) {
        $employees = [];
        foreach ($officeEmployees as $employeeName) {
            $employees[] = [
                'value' => key($employeeName),
                'text' => current($employeeName)
            ];
        }
    
        $officesOutput[] = [
            'text' => $name,
            'children' => $employees
        ];
    }
    
    
    echo json_encode($offices, JSON_PRETTY_PRINT);
    echo PHP_EOL;
    echo json_encode($officesOutput, JSON_PRETTY_PRINT);
    

    Results in:

    {
        "Office 1": [
            {
                "1": "Employee 1"
            },
            {
                "2": "Employee 2"
            },
            {
                "4": "Employee 4"
            }
        ],
        "Office 2": [
            {
                "3": "Employee 3"
            },
            {
                "5": "Employee 5"
            }
        ]
    }
    [
        {
            "text": "Office 1",
            "children": [
                {
                    "value": 1,
                    "text": "Employee 1"
                },
                {
                    "value": 2,
                    "text": "Employee 2"
                },
                {
                    "value": 4,
                    "text": "Employee 4"
                }
            ]
        },
        {
            "text": "Office 2",
            "children": [
                {
                    "value": 3,
                    "text": "Employee 3"
                },
                {
                    "value": 5,
                    "text": "Employee 5"
                }
            ]
        }
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器