/    Sign up×
Community /Pin to ProfileBookmark

Array With Count in Session

I’ve been using a session to store items for a shopping cart, making a “string” of ID’s all separated by a “/’ thats saved in my session.

When I add a item, I explode it into an array by the /. Then do an in_array check and array_push. Worked well.

I’m now wanting to keep a count as well. Maybe ID{Count}. for example 33{43]/61{1}/

This doesn’t work because splitting into an array doesn’t leave me with a unique array value as the count will vary.

Ideas please.

The issue is

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumAug 15.2020 — I recommend the following procedure:

Keep your information in an associative array where ID is the key and count is the value.

Then you can check if an ID already exists be isset().

AFAIK it's possible to store arrays in a session, no need for converting it to a string.

Apart from this when converting an array to a string, using the JSON format ist much more flexible, e. g. an ass. array or an array of multi dimensions can be converted by one call of json_encode.
Copy linkTweet thisAlerts:
@NogDogAug 15.2020 — > @Sempervivum#1622153 AFAIK it's possible to store arrays in a session, no need for converting it to a string.

Yep. This was my initial reaction: why store it as a string when you can store it as an array?

But if you really want to store it that way, then you could have fun with regular expressions:
<i>
</i>&lt;?php

$test = '1{2}/12{1}/123{4}';
preg_match_all('#(?:^|/)(d+){(d+)}#', $test, $matches);
print_r($matches);
// Array
// (
// [0] =&gt; Array
// (
// [0] =&gt; 1{2}
// [1] =&gt; /12{1}
// [2] =&gt; /123{4}
// )

// [1] =&gt; Array
// (
// [0] =&gt; 1
// [1] =&gt; 12
// [2] =&gt; 123
// )

// [2] =&gt; Array
// (
// [0] =&gt; 2
// [1] =&gt; 1
// [2] =&gt; 4
// )

// )
$data = [];
foreach($matches[1] as $ix =&gt; $id) {
$data[] = ['id' =&gt; $id, 'count' =&gt; $matches[2][$ix]];
}
print_r($data);
// Array
// (
// [0] =&gt; Array
// (
// [id] =&gt; 1
// [count] =&gt; 2
// )

// [1] =&gt; Array
// (
// [id] =&gt; 12
// [count] =&gt; 1
// )

// [2] =&gt; Array
// (
// [id] =&gt; 123
// [count] =&gt; 4
// )

// )
Copy linkTweet thisAlerts:
@NogDogAug 15.2020 — Actually, if there is some underlying reason to save it as a string instead of an array, I'd just build an array of data, then store it in a string via json_encode() and then use json_decode() when you want to read it back into an array.
Copy linkTweet thisAlerts:
@kiwisauthorAug 15.2020 — For some reason I thought you couldn't save an array in a SESSION.

Here's what I have.

I keep getting this error **"Notice: Undefined offset: 2 in C:wamp64wwwadd.php on line 26**"
``<i>
</i>&lt;?php
session_start();

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


if (isset($_GET['ID']) || isset($_GET['Cnt'])){
$ID = $_GET['ID'];
$Cnt = $_GET['Cnt'];

$ExpireTime = time() + 900;

if (isset($_SESSION['MyCart'])){

$CartArray[] = $_SESSION['MyCart'];
echo sizeof($CartArray);

$CartItem =[];
foreach($CartArray[0] as $ix =&gt; $id) {
var_dump($CartArray[0]);
$CartItem[] = ['id' =&gt; $id, 'count' =&gt; $CartArray[2][$ix]];
}



} else {

$newItem = ["ID"=&gt;$ID, "Cnt"=&gt;$Cnt];
$_SESSION['MyCart'] = $newItem;
$CartArray[] = $_SESSION['MyCart'];
}


print "&lt;pre&gt;";
print_r($CartArray);
print "&lt;/pre&gt;";


}
?&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumAug 15.2020 — Obviously that error message refers to this line:

`$CartItem[] = ['id' =&gt; $id, 'count' =&gt; $CartArray[2][$ix]];`

and says that there is no element having index 2 in the array.

I modified your code a bit according my recommendation in your other thread and it works fine for me:
&lt;?php
session_start();

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (isset($_GET['ID']) &amp;&amp; isset($_GET['Cnt'])) {
$ID = $_GET['ID'];
$Cnt = $_GET['Cnt'];

<i> </i>$ExpireTime = time() + 900;

<i> </i>if (isset($_SESSION['MyCart'])) {
<i> </i> $CartArray = $_SESSION['MyCart'];
<i> </i> if (isset($CartArray[$ID])) {
<i> </i> $CartArray[$ID] += $Cnt;
<i> </i> } else {
<i> </i> $CartArray[$ID] = $Cnt;
<i> </i> }
<i> </i>} else {
<i> </i> $CartArray = [$ID =&gt; $Cnt];
<i> </i>}
<i> </i>$_SESSION['MyCart'] = $CartArray;

<i> </i>print "&lt;pre&gt;";
<i> </i>print_r($CartArray);
<i> </i>print "&lt;/pre&gt;";

}
Copy linkTweet thisAlerts:
@kiwisauthorAug 15.2020 — Thanks, out of interest. When I look through like this. How can I get the ID. This echo's the count.

``<i>
</i>foreach($CartArray as $CartItem) {
echo $CartItem . "&lt;BR&gt;";

}<i>
</i>
``
Copy linkTweet thisAlerts:
@Moter12Aug 16.2020 — Finally it works
Copy linkTweet thisAlerts:
@SempervivumAug 16.2020 — That's very easy, you can acess both, the key and the value in the for loop:
foreach($CartArray as $ID =&gt; $Cnt) {
echo 'ID=' . $ID . ' Cnt=' . $Cnt' . "&lt;BR&gt;";
}
Copy linkTweet thisAlerts:
@Moter12Aug 30.2020 — @Moter12#1622176

[url=https://cellinfo.in/tubemate-apk/]TubeMate[/url]
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...