从数组中获取产品 ID

Get product Id's from Array

提问人:Sergei 提问时间:2/2/2021 最后编辑:Sergei 更新时间:2/2/2021 访问量:419

问:

我有php数组。我只需要在foreach中获取产品ID。我怎么能做到这一点?只需要一些产品ID列表。我如何解析 foreach 中的数组?我尝试过这样($numenclatureS['terminalGroupStopLists'][0]['items'] 作为$product)

Array
    (
        [correlationId] => 3b1ba055-c216-49de-9f85-209f68adf55b
        [terminalGroupStopLists] => Array
            (
                [0] => Array
                    (
                        [organizationId] => 7e3e9763-67a8-43ec-a663 9daf00f98945
                        [items] => Array
                            (
                                [0] => Array
                                    (
                                        [terminalGroupId] => 743a13f7-c0b1-41b6-b45a-2be970abff19
                                        [items] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [balance] => 0
                                                        [productId] => 137e576d-ac4e-4d60-8da2-9048e56c063a
                                                    )
    
                                                [1] => Array
                                                    (
                                                        [balance] => 0
                                                        [productId] => 06ea7ef2-8067-4b0b-9376-d1439a70f110
                                                    )
    
                                                [2] => Array
                                                    (
                                                        [balance] => -8
                                                        [productId] => 07040e16-7ae9-4f9e-a7c8-182ab89453f0
                                                    )
    
                                                [3] => Array
                                                    (
                                                        [balance] => 0
                                                        [productId] => e423da4a-b12e-4d93-a557-7cace4f565b9
                                                    )

    
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
PHP 数组 foreach

评论

1赞 mickmackusa 2/2/2021
stackoverflow.com/q/46275040/2943403,stackoverflow.com/a/10152943/2943403
1赞 Mohd Alomar 2/2/2021
这回答了你的问题吗?PHP 多维数组获取值

答:

0赞 Sergei 2/2/2021 #1

如果我这样做,它就会起作用

<?php 
$zzz = $numenclatureS['terminalGroupStopLists'];
$zzz1 = $zzz[0]['items'][0];
$zzz2 = $zzz1['items'];
?>


<?php foreach ($zzz2 as $product): ?>
<?php echo $product['productId']; ?>
<br />
<?php endforeach; ?>

也许有更简单的方法?

评论

1赞 Michael Berkowski 2/2/2021
如果 TerminalGroupStopLists 和项只有一个元素,则可以使用 php.net/manual/en/function.array-column.php[0]array_column($numenclatureS['terminalGroupStopLists'][0]['items'][0], 'productId')