How to store multiple values in array

CJAY

I want to add multiple values to array each time when i pass some values to this page(cart.php).

but now i am able to get only one value from array.i also tried

echo $_SESSION['cart'][1];
echo $_SESSION['cart'][2];

but i am able to get only one value which is newly added and i am getting notice

Notice: Undefined offset: 1
Notice: Undefined offset: 2

cart.php

<?php
    if(isset($_GET['action']) && isset($_GET['product_id'])){
            if($_GET['action'] == "add"){
                $product_id = $_GET['product_id'];
                $_SESSION['cart'] = array();
                array_push($_SESSION['cart'],$product_id);     
            }
        }
?>

this is how i am passing the values in query string

 http://localhost/mycart.php?action=add&product_id=4
Paladin

You always overwrite the array and so, you get only one value in it. Try this:

if(isset($_GET['action']) && isset($_GET['product_id'])){
            if($_GET['action'] == "add"){
                $product_id = filter_var($_GET['product_id'], FILTER_SANITIZE_NUMBER_INT);
                $_SESSION['cart'][] =$product_id;
            }
        }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to return multiple values in an Array - Excel VBA

分類Dev

How to update multiple values with an array inside Reducer

分類Dev

store values from a file into an array

分類Dev

Store multiple values for multiple times in one column?

分類Dev

array_unique or store values as keys in array

分類Dev

How to store WebElements in an array

分類Dev

Is it a good idea to store multiple values as SharedPreference?

分類Dev

How to store values in trigger PostgreSQL

分類Dev

How to import specific column from an excel sheet and store its values in a variable array in python?

分類Dev

How can I efficiently store pressed keys values in an array with SDL2?

分類Dev

How to store multiple byte arrays

分類Dev

Store multiple textareas in different array's index

分類Dev

Select multiple inputs and store into an array in angular 5

分類Dev

how to store all dictionary in an array

分類Dev

how to post array by store of sencha

分類Dev

How to store a number array in MongoDB

分類Dev

How to store file information into an array?

分類Dev

How to return multiple values

分類Dev

How to store inputs multiple times into local storage

分類Dev

How to split an array element with multiple values to get only one of them in PHP

分類Dev

Read file as bytes and store into an array of deterministically 8-bit values

分類Dev

RXJS Observable Transform Array To Multiple Values

分類Dev

Feeding multiple values in a loop to an array with VBA

分類Dev

Add/sum multiple values in array with same id

分類Dev

Having Multiple Values Inside One Array - For Loop?

分類Dev

MongoDB query to return multiple values from an array

分類Dev

Trying to set multiple array variables' values in Backstretch

分類Dev

Setting multiple values on one element in an array

分類Dev

How to filter multiple values in React

Related 関連記事

  1. 1

    How to return multiple values in an Array - Excel VBA

  2. 2

    How to update multiple values with an array inside Reducer

  3. 3

    store values from a file into an array

  4. 4

    Store multiple values for multiple times in one column?

  5. 5

    array_unique or store values as keys in array

  6. 6

    How to store WebElements in an array

  7. 7

    Is it a good idea to store multiple values as SharedPreference?

  8. 8

    How to store values in trigger PostgreSQL

  9. 9

    How to import specific column from an excel sheet and store its values in a variable array in python?

  10. 10

    How can I efficiently store pressed keys values in an array with SDL2?

  11. 11

    How to store multiple byte arrays

  12. 12

    Store multiple textareas in different array's index

  13. 13

    Select multiple inputs and store into an array in angular 5

  14. 14

    how to store all dictionary in an array

  15. 15

    how to post array by store of sencha

  16. 16

    How to store a number array in MongoDB

  17. 17

    How to store file information into an array?

  18. 18

    How to return multiple values

  19. 19

    How to store inputs multiple times into local storage

  20. 20

    How to split an array element with multiple values to get only one of them in PHP

  21. 21

    Read file as bytes and store into an array of deterministically 8-bit values

  22. 22

    RXJS Observable Transform Array To Multiple Values

  23. 23

    Feeding multiple values in a loop to an array with VBA

  24. 24

    Add/sum multiple values in array with same id

  25. 25

    Having Multiple Values Inside One Array - For Loop?

  26. 26

    MongoDB query to return multiple values from an array

  27. 27

    Trying to set multiple array variables' values in Backstretch

  28. 28

    Setting multiple values on one element in an array

  29. 29

    How to filter multiple values in React

ホットタグ

アーカイブ