gpt4 book ai didi

PHP foreach 中提供的参数无效

转载 作者:行者123 更新时间:2023-12-03 23:08:30 27 4
gpt4 key购买 nike

我试图用 foreach 循环读出这个嵌套数组,但收到错误“foreach 中提供的参数无效”

Array ( 
[regenerated] => 1302668837
[id] => 2
[qty] => 1
[price] => 1200
[name] => support
[optione] =>
[cart_contents] => Array (
[c4ca4238a0b923820dcc509a6f75849b] => Array (
[rowid] => c4ca4238a0b923820dcc509a6f75849b
[id] => 1
[qty] => 1
[price] => 29.95
[name] => Training DVD
[optione] =>
[subtotal] => 29.95
)
[c81e728d9d4c2f636f067f89cc14862c] => Array (
[rowid] => c81e728d9d4c2f636f067f89cc14862c
[id] => 2
[qty] => 1
[price] => 1200
[name] => support
[optione] =>
[subtotal] => 1200
)
[total_items] => 2
[cart_total] => 1229.95
)
[johndoe] => audio
[totalItems] => 2
)

$cart_contentz = $_SESSION['cart_contents'];

foreach($cart_contentz as $itemz => $valuez) {

foreach($valuez as $key1 => $value1) {

echo "$key1: $value1<br>";
}

最佳答案

主数组的第一层包含子数组的项目和一些不是子数组的项目。您的第二个循环不适用于非数组项。因此,您的代码应该是:

foreach($cart_contentz as $itemz => $valuez) {
if (is_array($valuez)) {
foreach($valuez as $key1 => $value1) {
echo "$key1: $value1<br>";
}
} else {
echo "$itemz: $valuez<br>";
}
}

关于PHP foreach 中提供的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5656969/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com