Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3781146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:54:17+00:00 2026-05-19T10:54:17+00:00

My shopping basket is an array and each item in it is also an

  • 0

My shopping basket is an array and each item in it is also an array.

At some points I am looping through each item looking for ID matches. When I have a match, I need to know the Items position in the main basket array so that i can perform updates and deletes.

Sounds very simple but I’m stuck on it.

I have this so far

//Lets say there are 5 items in this basket array (each item is also an array)
foreach ($_SESSION['basket'] as $basketArray){

        //this loops through the items attributes (size, colour etc)
        //when the ID is a match, i need to find out what position I am at in the main     array
        foreach($basketArray at $key = > $value){

             if ($value == $itemID){

                   //now I just need to know how to return 0, 1, 2, 3,  or 4 so that i can do 'unset' later.
             }
        }
}

Thanks for any help.

Ozzy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:54:17+00:00Added an answer on May 19, 2026 at 10:54 am

    Say this is your $_SESSION['basket']:

    Array
    (
        [0] => Array
            (
                [id] => 12
                [name] => some name
                [color] => some color
            )
    
        [1] => Array
            (
                [id] => 8
                [name] => some name
                [color] => some color
            )
    
        [2] => Array
            (
                [id] => 3
                [name] => some name
                [color] => some color
            )
    
        [3] => Array
            (
                [id] => 22
                [name] => some name
                [color] => some color
            )
    
    )

    First you need to loop through all the individual elements of the array $_SESSION['basket']:

    foreach ($_SESSION['basket'] as $i => $product) {
      /*
      $i will equal 0, 1, 2, etc.
      and is the position of the product within the basket array.
    
      $product is an array of itself, which will equal e.g.:
    
      Array
      (
          [id] => 12
          [name] => some name
          [color] => some color
      )
      */
    }

    Now you want to know if the id of a product matches the ID of the product you’re looking for. You don’t need to go through every element of the $product array to do that, assuming your ID will always be named “id”. Simply check the id field instead:

    foreach ($_SESSION['basket'] as $i => $product) {
      if ($product['id'] == $someId) {
        // at this point you want to remove this whole product from the basket
        // you know that this is element no. $i, so unset it:
    
        unset($_SESSION['basket'][$i]);
    
        // and stop looping through the rest,
        // assuming there's only 1 product with this id:
        break;
      }
    }

    Notice that there’s also a danger to checking the values, and not the keys. Say you have a product that’s build up like so:

      Array
    (
        [count] => 12
        [id] => 5
        [name] => some name
        [color] => some color
    )

    If you go through all the values, like your doing now, and try to match that to a certain id, what happens when this id happens to be “12”?

    // the id you're looking for:
    $someId = 12;
    
    foreach ($product as $key => $value) {
      // first $key = count
      // first $value = 12
      if ($value == $someId) {
        // ...
        // but in this case the 12-value isn't the id at all
      }
    }

    So: always reference a specific element from the array, in this case “id” (or whatever the name is that you used in your app). Don’t check random values, since you can’t be absolutely certain that when it matches, this is in fact the correct value you’re looking for.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a shopping basket, and some javascript methods for real time updating number
I have a virtual store and a shopping basket. Also, I have in every
What i need a simple shopping basket written in ASP . I have looked
I have the following code which is used to slidein/out a div (shopping basket)
I have a repeater, which represents a shoppingbasket. This shopping basket has an Update
i have a javascript shopping basket, in which the sum is returning a NaN
i have written a shopping basket using js, and i am updating the total
I have a table which holds the products in a shopping basket (stowaway_orders) and
I have a MySQL shopping basket set up. There are 11 products in total
I need to add up a total for a shopping basket but I dont

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.