我只是想在数组中添加我的会话值,但它只显示最后一个elememt这里是我的代码

时间:2016-12-07 06:27:32

标签: php arrays session

我只想在数组中添加会话值,但它只显示最后一个元素。这是我的代码

if(isset($_SESSION["cart_products"])) {
    foreach ($_SESSION["cart_products"] as $cart_item)
    {
        $p_name = $cart_item["info2"];

    }
    echo $p_name;
}

$product_files[] = array( 
    'name' => $p_name,
    'filename' => $p_name,
    'source'=> $c_path                                                                               );

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作

if(isset($_SESSION["cart_products"])) {
 $args = array();
 foreach ($_SESSION["cart_products"] as $cart_item){
   $args[] = $cart_item["info2"];
 }
 print_r($args);

答案 1 :(得分:0)

<强>更新

         if(isset($_SESSION["cart_products"])) {
            $p_name = array();

            foreach ($_SESSION["cart_products"] as $cart_item)
            {

                $p_name = $cart_item["info2"];
                  $product_files[] = array( 
                    'name' => $p_name ,
                    'filename' => $p_name,
                    'source'=> $c_path);
            }      
        }

更新了圈内的$ product_files