操纵会话多阵列

时间:2011-07-02 05:09:51

标签: php

我想操纵我的Array中的每个变量。我不知道如何为每个数组取一个值。

    <?php
    session_start();
    /*array  name, howmuch, cost*/
    $_SESSION['ID'][] = array("Soup", 3 , 1.25);
    $_SESSION['ID'][] = array("Puos", 1 , 3.25);

    foreach ($_SESSION['ID'] as $product=>$value){

        foreach ($value as $var)
       {
           /*HERE echo ("UR product is ". name );
                  echo (howmuch);
                  echo (costo*0.40 );  */
        }
     echo ("<br>"); }
      session_destroy();   ?>

Grettings all。

2 个答案:

答案 0 :(得分:2)

// loop with $product as a reference
foreach ($_SESSION['ID'] as &$product) {
   // 60% discount on every price
   $product[2] *= 0.4;
}

// use full path to each value to be changed
foreach ($_SESSION['ID'] as $key=>$product) {
   // 60% discount on every price
   $_SESSION['ID'][$key][2] *= 0.4;
}

答案 1 :(得分:0)

foreach ($_SESSION['ID'] as $value) {
    // $value will be array("Soup", 3 , 1.25), for example
    echo $value[0];
    echo $value[1];
    echo $value[2];
}