【问题标题】:PHP how to get specific value from array and store in `$_SESSION`PHP如何从数组中获取特定值并存储在`$_SESSION`中
【发布时间】:2023-03-19 09:11:01
【问题描述】:

我有一个数组,我想在其中获取特定值(“已创建”),然后将 $_SESSION 存储在一个新数组中。我怎样才能做到这一点?

我的数组是这样的

Array
    (
        [0] => Array
            (
                [product_id] => 679
                [quantity] => 1
                [created] => 2018-10-01
            )

        [1] => Array
            (
                [product_id] => 677
                [quantity] => 1
                [created] => 2018-10-05
            )

        [2] => Array
            (
                [product_id] => 678
                [quantity] => 1
                [created] => 2018-10-03
            )

    )

我尝试了类似的方法:

foreach($created as $i) {
   $values = [$i]['created'];
   echo $values;
}

session_start();
$_SESSION['creation_dates'] = $values;

但这不起作用。

谁能帮帮我?

【问题讨论】:

    标签: php arrays session


    【解决方案1】:

    您需要首先开始您的会话,然后再进行任何输出。您的echos 正在阻止这种情况。您一定会收到 Notices 但看不到它们,这取决于您的 error_reporting

    试试这个代码:

    session_start();
    
    $values = array();
    foreach($creation_dates as $i) {
       $values[] = $i['created'];
    }
    
    $_SESSION['creation_dates'] = $values;
    
    print_r($values);
    print_r($_SESSION['creation_dates']);
    

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 1970-01-01
      • 2013-04-18
      • 2014-09-28
      • 2021-11-28
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多