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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?