How to write to the session array, the next plan

Array( [0] => Array ( [ID] => 2041329 [NAME] => Apple [PRICE] => 49900.00 [QUANTITY] => 1 ) [1] => Array ( [ID] => 2041331 [NAME] => TV [PRICE] => 11800.00 [QUANTITY] => 1 ) 

I make the recording as follows:

  foreach($items as $k => $val){ $_SESSION['items'] = $val; } 

But not all values ​​appear in the session, tell me how to correct this situation

  • 2
    well, try $_SESSION['items'][] = $val; chtoli - SLy_huh
  • so nothing ever got - ChromeChrome
  • four
    Unlikely, okay. And why not just equate $_SESSION['items'] = $items ? - SLy_huh
  • @SLy_huh is not "unlikely", but the only meaningful option. - Ipatiev

3 answers 3

You initially lack knowledge of how arrays are organized in PHP, and what the $ _SESSION variable in it is. As you have already noticed right here, you can simply, without any cycles, write $_SESSION['items'] = $items; , and after receiving the saved goods from the basket (or what you have there for the list of goods stored), going through $_SESSION['items'] as an array. The code will be something like this:

 foreach ($_SESSION['items'] as $item) { echo $item['ID']; echo $item['NAME']; /* ... */ } 

if you suddenly want to address directly, it can be done like this:

 $_SESSION['items'][0]['ID']; $_SESSION['items'][0]['NAME']; /* ... */ $_SESSION['items'][1]['ID']; $_SESSION['items'][1]['NAME']; /* ... */ 

or so:

 $arr = $_SESSION['items']; $arr[0]['ID']; $arr[0]['NAME']; /* ... */ $arr[1]['ID']; $arr[1]['NAME']; /* ... */ 

Link for compulsory study: http://php.net/manual/ru/language.types.array.php But generally I would advise you to look at / read some kind of training materials in the style of php for dummies, you will certainly find out a couple of unexpected things.

And yes, arrays in PHP are somewhat different from arrays in many other programming languages, if you don’t want to step on a rake every time that is described here - it’s better to spend a day of time on the tip in the paragraph above.

    So it can work too. $_SESSION['items']=$items;

    • one
      Are you serious? - Alexey Shimansky
    • @ Alexey Shimansky, yes, seriously, and what side did Alexey embarrass you? - fonjeekay
    • the first foreach for [1] => Array and the second for [ID] => 2041331..... each element. This is an approximate code. - fonjeekay
    • I was embarrassed by absolutely everything. Starting from the fact that the cycles can not be used at all and ending with the fact that you (either you don’t know, or for some other reason) use the foreach bike with the $i counter instead of trivially for ..... you hope it ( foreach) you don’t shove it absolutely everywhere and you know about the existence of other types of cycle ..... But this is a bicycle crutch still that, I tell you ...)) - Aleksey Shimansky
    • @ Alexey Shimansky, as far as I understand the question that was asked by the distinguished @ChromeChrome, in the first block he has an array without a terminating bracket (the array is not closed). That is, an array can have not only zero 0 and first 1 iteration (as shown in the first code), but also have 100 or more such elements, and therefore the cycle is NECESSARY. Regarding the counter, I do not consider the iteration counter in the foreach error. But your opinion has been taken into account. - fonjeekay

    This happened because you wrote down each element of the $ items array in $ _SESSION ['items'], so the last element of the array was written there.

     $_SESSION['items'][] = $val; //вот так правильно, только $_SESSION['items'] заранее нужно объявить пустым массивом 

    It's easier to write them into the session like this:

     $_SESSION['stored_items'] = $items; 
    • one
    • @alexanderbarakin you have failed) - Alexey Shimansky
    • Who, well, who here raises meaningless answers? As on the English site, the epidemic began here "to give a point just for being able to write the answer"? - Ipatiev
    • @ Alexey Shimansky in what sense? A user with the same name but a different account edited this question. - Ipatiev
    • @Ipatiev didn’t see editing ... I thought alexander barakin saw in the answer "This was because" and decided that it was the second random author of the question - Alexey Shimansky