开发者

PHP invalid argument supplied in foreach

开发者 https://www.devze.com 2023-02-24 06:27 出处:网络
I am trying to read out this nested array with a foreach loop but开发者_JAVA百科 get an error \"invalid argument supplied in foreach\"

I am trying to read out this nested array with a foreach loop but开发者_JAVA百科 get an error "invalid argument supplied in 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>"; 
}


the first level of your main array has items that are sub-arrays and some that are not. Your second loop doesn't work on non-array items. Thus, your code should be:

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


you'll need to load that array into your $_SESSOIN['cart_contents'] which may have been done. secondly, your inner foreach is acting on the values of that array which are not arrays. I'm fairly certain that the inner foreach is causing your woes. Also, your Array may just be for illustrating what's in $_SESSION['cart_contents'], but adding quotation marks instead of square brackets around the keys will make it more uniform and easier to read.

Update:

after seeing the reformatted code, thanks @AgentConundrum, now I can more clearly see the issue. Try adding an if(is_array($valuez)) around your inner foreach.


Maybe to use recursion:

 function printArray($array, $parent=false, $level=0) {
   if (!($parent === false)) echo "<b>".str_pad('',($level-1)*4,"-")."[$parent] =></b><br />\n";
   foreach ($array as $key=>$value) {
     if (!is_array($value)) echo str_pad('',$level*4,"-")."[$key] => $value<br />\n";
     else printArray($value, $key, $level+1);
     }
   }

 print_array($your_array);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号