array_push无法识别我的数组

时间:2018-01-12 14:02:23

标签: php array-push

我有以下PHP代码生成以下错误:

  

警告:array_push()期望参数1为数组,在第393行/mnt/web109/e2/04/57677304/htdocs/wschaijk/projects/p5/advancedbuilder.php中给出null

if(isset($_POST['getAdvancedResults']))
        {
            $errorsarray = array();
            $continue = true;
            $controller = new ProductController();
            // Make a boolean that checks whether the query should be executed or not
            // If an error is found, this boolean becomes false and the query will not be run
            // Do check all field and add error message to an array using the array_push() function
            // After that, check using the boolean whether the query shall be run
            // Else: call print_form() and give it the array with error messages as argumen

            //cpu mobo compatbility check
            $CPU_comp = $controller -> get_single_entities(array("Id" => $_SESSION["advanced_builder"]["CPU"]))[0] -> get_compatibility();
            $MOBO_comp = $controller -> get_single_entities(array("Id" => $_SESSION["advanced_builder"]["Moederbord"]))[0] -> get_compatibility();
            //["LGA1151", "DDR4"] en ["LGA1151", "ATX", "DDR4"]
            $each_cpu_comp = explode(", ", $CPU_comp);
            $each_mobo_comp = explode(", ", $MOBO_comp);
            foreach ($each_cpu_comp as $comp)
            {
                foreach ($each_mobo_comp as $othercomp)
                {
                    //ram:
                    if ($comp != $othercomp && strpos($comp, "DDR") && strpos($othercomp, "DDR")) //DDR4 tegen DRR3 ->
                    {
                        $continue = false;
                        array_push($errorsarray, "RAM van CPU is niet compatible met RAM van het mnoederbord");
                    }
                    //socket
                    if ($comp != $othercomp && ((strpos($comp, "LGA") && strpos($othercomp, "LGA")) || (strpos($comp, "AM") && strpos($othercomp, "AM"))))
                    {
                        $continue = false;
                        array_push($errorsarray, "Socket van het moederbord en de cpu komen niet overeen");
                    }
                }
            }

            if ($continue)
            {
                    echo "<div class='row' style='text-align: center; margin-top: 5%;'>
                                  <div class='col-12'>
                                      <h3 style='color: green'>Systeem volledig compatibel! De correcte producten zijn aan uw winkelmand toegevoegd.</h3>
                                  </div>
                              </div>";
                    foreach ($_SESSION["advanced_builder"] as $product)
                    {
                        array_push($_SESSION["shoppingcart"], $product);
                    }
            }
            else
            {
                print_form($errorsarray);
            }
        }
        else
        {
            print_form();

        }

当我尝试运行代码时,我得到一个错误,说我试图将值插入的数组是null。我应该做些什么来让array_push再次工作?

我尝试过的事情: - 使变量$ errorsarray全局化 - 在if(isset ...

)之外创建变量$ errors数组

2 个答案:

答案 0 :(得分:0)

我猜第393行(错误说明)如下:

array_push($_SESSION["shoppingcart"], $product);

所以你必须确保$_SESSION["shoppingcart"]是一个数组。

可能的解决方案

$errorsarray = array();下方,您可以写下以下内容:

$_SESSION["shoppingcart"] = array();

答案 1 :(得分:0)

在代码的开头,foreach语句之前,您必须定义errorsarray

如果你看看php manual,你可以看到你需要以这种简单的方式定义空数组:

$errorsarray = array();

如果错误引用$_SESSION变量上的arra_push,则必须检查此代码前是否有session_start();行。

我认为你必须在foreach语句之前定义,$_SESSION["shoppingcart"]也是一个空数组:

$_SESSION["shoppingcart"] = array();
相关问题