aebstract Posted May 24, 2010 Share Posted May 24, 2010 I have an array/session set. $_SESSION[cart] It's in this format: Array ( [0] => Array ( [qty] => 1 [cat] => EZ Street [file] => 844900751_iDZdR-M.jpg ) ) When I go to add another value to the array/session, I need to be able to look through and check to see if it's already set based on cat & file. If a record already exists, I just need to add +1 to the qty. Right now I got foreach($_SESSION[cart] AS $key => $value){ foreach($_SESSION[cart][$key] AS $key => $value){ echo "$key $value"; }} This to just get me my values, but I'm kind of stuck trying to figure out how to check it, and then if it already exists add the +1 and kill the rest of the add script. Link to comment https://forums.phpfreaks.com/topic/202729-checking-for-duplicate-in-array/ Share on other sites More sharing options...
ScotDiddle Posted May 24, 2010 Share Posted May 24, 2010 aebstract, The following code should do the trick... Scot L. Diddle, Richmond VA <?php Header("Cache-control: private, no-cache"); Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); Header("Pragma: no-cache"); $arrayToCheck = Array( 0 => Array ( 'qty' => 1, 'cat' => 'EZ Street', 'file' => '844900751_iDZdR-M.jpg' ), 1 => Array ( 'qty' => 1, 'cat' => 'Hard Street', 'file' => '844900751_iDZdR-hard.jpg' ) ); $_POST['incomingArray'] = array( 0 => Array ( 'qty' => 1, 'cat' => 'EZ Street', 'file' => '844900751_iDZdR-M.jpg' ) ); $arrayOut = $arrayToCheck; foreach ($arrayToCheck as $cartIDX => $cartItem) { /** * * $cartItem should look like this: * */ // Array // ( // [qty] => 1 // [cat] => EZ Street // [file] => 844900751_iDZdR-M.jpg // ) // $cartCat = $cartItem['cat']; $incomingCart = $_POST['incomingArray'][$cartIDX]['cat']; if ($incomingCart != NULL) { if ($cartCat == $incomingCart) { $arrayOut[$cartIDX]['qty']++; } else { $arrayOut[] = $_POST['incomingArray'][$cartIDX]; } } } $arrayToCheck = $arrayOut; $display = TRUE; if ($display) { echo "<pre> \n"; print_r($arrayToCheck); echo "</pre> \n"; } ?> Link to comment https://forums.phpfreaks.com/topic/202729-checking-for-duplicate-in-array/#findComment-1062604 Share on other sites More sharing options...
ScotDiddle Posted May 24, 2010 Share Posted May 24, 2010 aebstract, Silly me. Who just orders one of anything. The re-posted code below handles whatever new qty the user requests. <?php Header("Cache-control: private, no-cache"); Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); Header("Pragma: no-cache"); $arrayToCheck = Array( 0 => Array ( 'qty' => 1, 'cat' => 'EZ Street', 'file' => '844900751_iDZdR-M.jpg' ), 1 => Array ( 'qty' => 1, 'cat' => 'Hard Street', 'file' => '844900751_iDZdR-hard.jpg' ) ); $_POST['incomingArray'] = array( 0 => Array ( 'qty' => 3, // Note the New Qty... 'cat' => 'EZ Street', 'file' => '844900751_iDZdR-M.jpg' ) ); $arrayOut = $arrayToCheck; foreach ($arrayToCheck as $cartIDX => $cartItem) { /** * * $cartItem should look like this: * */ // Array // ( // [qty] => 1 // [cat] => EZ Street // [file] => 844900751_iDZdR-M.jpg // ) // $cartCat = $cartItem['cat']; $incomingCart = $_POST['incomingArray'][$cartIDX]['cat']; $incomingQty = $_POST['incomingArray'][$cartIDX]['qty']; // Added if ($incomingCart != NULL) { if ($cartCat == $incomingCart) { $arrayOut[$cartIDX]['qty'] += $incomingQty; // Changed from $arrayOut[$cartIDX]['qty']++ } else { $arrayOut[] = $_POST['incomingArray'][$cartIDX]; } } } $arrayToCheck = $arrayOut; $display = TRUE; if ($display) { echo "<pre> \n"; print_r($arrayToCheck); echo "</pre> \n"; } ?> Link to comment https://forums.phpfreaks.com/topic/202729-checking-for-duplicate-in-array/#findComment-1062608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.