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();   ?>
1
  • This is not a well described question. You say that you want to manipulate the data, but you are only echoing data in your commented-out code. The accepted answer mutates the input array, but the question is not clear in this requirement. Commented 2 days ago

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.

1 Comment

The first example looks cleaner, but sometimes you have to be careful with references. I suggest you read the PHP manual section about them.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.