dpzr52746 2017-08-10 07:24
浏览 37
已采纳

如何从PHP中的另一个数组中提取数组[关闭]

I have an array:

Array
(
    [0] => Array
        (
            [filePath] => 0000000023.jpg
        )

    [1] => Array
        (
            [0] => Array
                (
                    [Label] => Array
                        (
                            [0] => Person
                            [1] => Person
                        )

                    [Score] => Array
                        (
                            [0] => 0.999217033
                            [1] => 0.999318361
                        )

                    [Coordinates] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 338
                                    [1] => 430
                                    [2] => 307
                                    [3] => 506
                                )

                            [1] => Array
                                (
                                    [0] => 71
                                    [1] => 104
                                    [2] => 318
                                    [3] => 483
                                )

                        )

                )

        )

)

I need to make it to be look like this:

Array
(
    [0] => Array
        (
            [filePath] => 0000000023.jpg
        )

    [1] => Array
        (
            [0] => Array
                (
                    [Label] => Person
                    [Score] => 0.999217033
                    [Coordinates] => 
                        [0] => Array
                                (
                                    [0] => 338
                                    [1] => 430
                                    [2] => 307
                                    [3] => 506
                                )
                )
            [1] => Array                     
                (
                    [Label] => Person
                    [Score] => 0.999318361
                    [Coordinates] => 
                        [0] => Array
                                (
                                    [0] => 71
                                    [1] => 104
                                    [2] => 318
                                    [3] => 483
                                )
                )

        )

)

In Ruby I can use map and select to get the result however in PHP I can't figure it out. I managed to do it with Foreach loop however it is very ugly and lots of if statement.

I'd appreciate any guidance.

  • 写回答

2条回答 默认 最新

  • dousao6313 2017-08-10 07:50
    关注

    It can be done with foreach, but no IF's. I think it's not useful to use PHP mapping or other array functions for such thing (although)

    Check this:

    $tab = [
        [
            'filePath' => '0000000023.jpg'
        ],
        [
            [
                'Label' => [
                    'Person',
                    'Person'
                ],
                'Score' => [
                    0.999217033,
                    0.999318361
                ],
                'Coordinates' => [
                    [338, 430, 307, 506],
                    [71, 104, 318, 483]
                ]
            ]
        ]
    ];
    
    $returnTab[0] = $tab[0];
    
    foreach ($tab[1][0]['Label'] as $pos => $label) {
        $returnTab[1][] = [
            'Label' => $label,
            'Score' => $tab[1][0]['Score'][$pos],
            'Coordinates' => $tab[1][0]['Coordinates'][$pos],
        ];
    }
    var_dump($returnTab);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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