0

I wanna to manipulate each variable inside my Array. I dont know how take a value for each 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 Answers 2

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

or

// 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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

great. its simple really do. I was expect [var] or something XD.
The first example looks cleaner, but sometimes you have to be careful with references. I suggest you read the PHP manual section about them.
ok good, thank you. it will help me, because, i didnt see about validate stuff.
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];
}

1 Comment

good, but i cant manipulate variable , just print like a String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.