代码之家  ›  专栏  ›  技术社区  ›  ST80

PHP如何从数组中获取特定值并存储在`$\u会话中`

  •  1
  • ST80  · 技术社区  · 8 年前

    我有一个数组,我想在其中获取一个特定的值(“created”),然后存储一个im $_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;
    

    但这行不通。

    1 回复  |  直到 8 年前
        1
  •  2
  •   msg    8 年前

    在任何输出之前,首先需要启动会话。你的 echo 我们正在阻止。你一定是疯了 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']);