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条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题