• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

how to remove a array elements


i have a 2 d array i need to remove particular array depending on the user input

print_r($_SESSION['basket']) gives the following

Array ( [0] => Array ( [0] => SCAN ABDOMEN [1] => 120.00 [2] => 291 ) [1] => Array ( [0] => DENGUE IGG AND IGM [1] => 30.00 [2] => 178 ) [2] => Array ( [0] => OCCULT BLOOD [1] => 90.00 [2] => 153 ) )

what i need i the user will give last value i.e 291 or 178 or 153 in the array

when he gives 192 i want the entire corresponding array
 [0] => SCAN ABDOMEN [1] => 120.00 [2] => 291 to be deleted leaving the rest

similarly if he gives 178 that corresponding array....

in my case what happens is when i try deleting  3rd array 2 nd array is getting removed and when i try removing 2nd array first one is getting removed...

when when i remove the first one its getting removed properly..... in all the other cases its like as i have said above....

if ($_POST['removeItem'] != "") 
function FindBasket($id) //find product position 
		return array_search($id, $_SESSION['basket']);

Open in new window

2 Solutions
that cause your looking for  291 or 178 or 153 and the array indexes are arrays them self,

maybe you should just search the slow way

function FindBasket($id) //find product position 
  for ($x=0, $x<sizeof($_SESSION['basket'], $x++)
    if ($id== $_SESSION['basket'][$x][2])
      return $x;
  return false;

Open in new window


try this one:
foreach ($_SESSION['basket'] as $key => $item)
    if ($item['2'] == $_POST['removeItem'])

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now