Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我只想在数组中添加会话值,但它只显示最后一个元素。这是我的代码_Php_Arrays_Session - Fatal编程技术网

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

Php 我只想在数组中添加会话值,但它只显示最后一个元素。这是我的代码,php,arrays,session,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_nam

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

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                                                                               );

您可以执行以下操作

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

已更新

         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\u文件

这是因为在
foreach
循环之后使用了
echo$p\u name
。还有,$cart\u item[“info2”]`中的
s是什么?数组?是否使用session_start()?是否回显$cart_item[“info2”];然后创建一个数组。你到底想打印什么?