【问题标题】:Function each() in PHPPHP 中的 each() 函数
【发布时间】:2023-03-22 10:33:01
【问题描述】:
<?php
require_once("entities/product.class.php");
require_once("entities/category.class.php");
$cates = Category::list_category();

session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');

if(isset($_GET["id"])){
    $pro_id = $_GET["id"];
    $was_found = false;
    $i = 0;
    
    if(!isset($_SESSION["cart_items"]) || count($_SESSION["cart_items"])<1){
        $_SESSION["cart_items"] = array(0 => array("pro_id" => $pro_id, "Quantity" => 1));
    } else{
        foreach($_SESSION["cart_items"] as $item){
            $i++;
            while(list($key,$value) = each($item)){
                if($key=="pro_id" && $value==$pro_id){
                    array_splice($_SESSION["cart_items"], $i-1, 1, array(array("pro_id" => $pro_id, "Quantity" => $item["Quantity"]+1)));
                    $was_found = true;
                }
            }
        }
        if($was_found==false){
            array_push($_SESSION["cart_items"], array("pro_id" => $pro_id, "Quantity" =>1));
        }
    }
    header("location: shopping_cart.php");
}?>

这是我的代码。但是 while 循环中的 each() 函数是错误的。

从数组中返回当前键值对并前进数组光标 每个(数组 $array ):数组 未定义函数'每个'.intelephense(1010)

请帮帮我。

【问题讨论】:

  • each() 已弃用。

标签: php each


【解决方案1】:

each() 已弃用。不要再使用它了。请改用foreach()

替换

while(list($key,$value) = each($item)) {

foreach($item as $key => $value) {

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2016-03-13
    • 2012-09-10
    • 2018-12-16
    • 1970-01-01
    • 2018-04-16
    • 2010-10-12
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多