/    Sign up×
Community /Pin to ProfileBookmark

Final Array Question

This one has done my head in for hours and I don’t seem to be any closer to a fix.

What I’m doing is the following.
-If a GET ID and Count as passed
-See if a session is set. If not create one and pass in a single array. This part seems to work.

-If it is set get the value which is an array and check to see if there is an sub array with an ID in there.

  • If it’s in there update the count in the array item within the array but the passed in Cnt value.
  • Update the main array and save it back to a session

    “`
    if (isset($_GET[‘ID’]) || isset($_GET[‘Cnt’])){
    $ID = $_GET[‘ID’];
    $Cnt = $_GET[‘Cnt’];

    $ExpireTime = time() + 900;

    if (isset($_SESSION[‘MySite’])){

    $CartArray[] = $_SESSION[‘MySite’];
    echo sizeof($CartArray) .”<BR>”;

    print “<pre>”;
    print_r($CartArray);
    print “</pre>”;

    $found = false;
    foreach ($CartArray as $CartItem => &$value) {
    if ($CartItem[‘ID’] == $ID) {
    $CartItem[‘Cnt’] = $CartItem[‘Cnt’] + $Cnt;
    $found = true;
    break;
    } else {
    $newItem = [“ID”=>$ID, “Cnt”=>$Cnt];
    array_push($CartArray, $newItem);
    $_SESSION[‘MySite’] = $CartArray;
    $CartArray = $_SESSION[‘MySite’];
    break;
    }
    }

    print “<pre>”;
    print_r($CartArray);
    print “</pre>”;

    } else {

    $newItem = [“ID”=>$ID, “Cnt”=>$Cnt];
    $_SESSION[‘MySite’] = $newItem;
    $CartArray[] = $_SESSION[‘MySite’];
    }

    echo “<hr>”;
    print “<pre>”;
    print_r($CartArray);
    print “</pre>”;

    }
    “`

    to post a comment
    PHP

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @SempervivumAug 15.2020 — Please don't open new threads for the same subject repeatedly. Continue here:

    https://www.webdeveloper.com/d/390999-array-with-count-in-session/5
    Copy linkTweet thisAlerts:
    @SempervivumAug 15.2020 — {"locked":true}
    ×

    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,
    )...