Jump to content

checking for duplicate in array


aebstract

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.