空数组错误


Empty arrays error

我有一组PHP数组,我正在尝试检索这些细节和回显。 但是我收到错误,例如Warning: Invalid argument supplied for foreach().如何纠正此类错误的出现?当我回显所有用户的所有蛋糕时,会出现此问题。

到目前为止,我已经尝试从代码中消除可能的错误,并且还使用了验证语句,例如 if empty 然后执行此操作,但是问题仍然存在。

<?php 
//USERS ARRAYS
$users = array();
$users["john"] = array(
    "first name" => "John",
    "last name" => "Smith",
    "address" => "London");
$users["jade"] = array(
    "first name" => "Jade",
    "last name" => "Anthony",
    "address" => "Essex");
$users["clement"] = array(
    "first name" => "Clement",
    "last name" => "Smith",
    "address" => "Essex");
//CAKES ARRAY
$cakes = array();
$cakes = array(
    "john" => array(
        "Chocolate Cake" => array(
                    "ingredients" => "chocolate, flour, eggs",
                    "cooking time" => "20 minutes"),
        ),
    "jade" => array(
        "Lemon Cheesecake" => array(
                    "ingredients" => "lemons, flour, eggs",
                    "cooking time" => "30 minutes")),
    "clement" => NULL,
);
?>
<?php $user_name = 'john';?>
<h1>Name</h1>
    <p><?php echo $users["first name"]; ?></p>
<h1>Cakes Made</h1>
<?php
foreach($cakes[$user_name] as $key => $val)
 { echo $key . " <strong>Ingredients: </strong>" . $val["ingredients"] . "<br>"; }
    ?>
<h1>All cakes from all users</h1>
<?php foreach($cakes as $cake)  {  ?>
<?php if(is_array($cake)) {
foreach($cake as $cakemade => $newval)
  if(empty($cakemade)) { echo "";} else
 { echo $cakemade . "<strong>" . $newval['ingredients'] ."</strong> <br>"; }
?>
<?php }} ?>

欢迎您的意见。:)

编辑:为了清楚起见,代码错误与代码的第二部分有关,其中显示所有用户的所有蛋糕。

您可以测试是否设置了 want use in foreach 中的变量

if(isset($cakes[$user_name]) && count($cakes[$user_name]) > 0)
  foreach($cakes[$user_name] as $key => $val) {}

缺少keys" s 。您的阵列应该是 -

$cakes = array(
    "john" => array(
        "Chocolate Cake" => array(
                    "ingredients" => "chocolate, flour, eggs",
                    "cooking time" => "20 minutes"),
        )
    "jade" => array(
        "Lemon Cheesecake" => array(
                    "ingredients" => "lemons, flour, eggs",
                    "cooking time" => "30 minutes"),
    "clement" => NULL,
);
if(is_array($variable) && count($variable) > 0) {
     foreach($variable as $key => $value) {
        //Your code
     }
)

粘贴的代码中有许多语法错误。 还可以使用 is_array() 来检查数组。此外,您还定义了$username = "john"并传递了带有$user_name尝试

<?php 
//USERS ARRAYS
$users = array();
$users["john"] = array(
    "first name" => "John",
    "last name" => "Smith",
    "address" => "London");
$users["jade"] = array(
    "first name" => "Jade",
    "last name" => "Anthony",
    "address" => "Essex");
$users["clement"] = array(
    "first name" => "Clement",
    "last name" => "Smith",
    "address" => "Essex");
//CAKES ARRAY
$cakes = array();
$cakes = array(
    "john" => array(
        "Chocolate Cake" => array(
                    "ingredients" => "chocolate, flour, eggs",
                    "cooking time" => "20 minutes"),
        ),
    "jade" => array(
        "Lemon Cheesecake" => array(
                    "ingredients" => "lemons, flour, eggs",
                    "cooking time" => "30 minutes")),
    "clement" => NULL,
);
?>
<?php $user_name = 'john';?>
<h1>Name</h1>
    <p><?php echo $users["first name"]; ?></p>
<h1>Cakes Made</h1>
<?php
foreach($cakes[$user_name] as $key => $val)
 { echo $key . " <strong>Ingredients: </strong>" . $val["ingredients"] . "<br>"; }
    ?>
<h1>All cakes from all users</h1>
<?php foreach($cakes as $cake)  {  ?>
<?php if(is_array($cake)) {
foreach($cake as $cakemade => $newval)
  if(empty($cakemade)) { echo "";} else
 { echo $cakemade . "<strong>" . $newval['ingredients'] ."</strong> <br>"; }
?>
<?php }} ?>

可能最好的方法是检查变量是否为数组,然后检查它是否为空。正如您已经明确指出的那样,如果变量是数组并且它不为空,那么您应该浪费处理器时间来迭代数组。

所以代码看起来像这样:

if(!empty($var) && is_array($var)) {
    foreach($var as $key => $value) {
        echo $key . ' => ' . $value;
    }
}