shopping cart session array - php

When user click on the add button, the product id are stored into the session array.
See Code below:
Array
(
[storeID] => 123
[10] => Array
(
[quantity] => 1
[product_id] => 2
[extras_id] => Array
(
)
)
[20] => Array
(
[quantity] => 12
[product_id] => 2
[extras_id] => Array
(
8
)
)
)
As you can see 10 and 20 is option_id from the product_id = 2
User can select number of options from a specific product.
User can select extras (or without) from option
Is this array good design or how can it be improved?
Example:
Product (2): Burger
- Option (10): Large (User not selected any extra)
- Option (20): Small (User selected coke(8) as extra)
User selected ID 10 and 20 for burger.

I don't see a problem with it, except that you could get it more "organized", this way:
Array
(
[123] => array(
[2] => array(
[10] => array(
[quantity] => '',
[extras] => ''
),
[20] => array(
[quantity] => '',
[extras] => ''
)
)
)
)
But that's just my opinion and my way to think.

What if product 3 also has option 10?
I'd go for using product's as the key, and adding quantity, options and extras an subs of that array.
This setup does assume you can't add the same product more then once, even if the extra's do not match. Another poster suggest adding an combination of extras/options/productid, which is good. If that's the case, please upvote him :).
Array
(
'cart' => array(
'storeid' => 123,
'products' => array(
2 => array(
'quantity' => 2,
'options' => array(10, 20),
'extras' => array(2)
),
3 => array(
'quantity' => 12,
'options' => array(150, 20),
'extras' => array(1, 7)
)
)
)
)

What I prefer is to store products in this format:
Array
(
[md5 hash of (product id + serialized array of selected options)] => Array
(
'qty' => 10
'title' => 'Product XYZ'
'price' => 49.99
'options' => Array
(
...
)
)
)
That gives each cart item it's own "id", easily allowing you to modify data for each cart item, such as qty, if you need to.
Edited:
This is a trimmed down example of what my cart's items array looks like:
7483f8f0007eb9ef3ddb8d2bff606bd6 and 859d1bb225ba5d16de4d4c23076cfae0 are both md5 hashes created by md5($itemId.serialize($submittedOptions)).
Array
(
[7483f8f0007eb9ef3ddb8d2bff606bd6] => Array
(
[id] => 3
[qty] => 2
[price] => 20.00
[title] => Product XYZ
[data] => Array
(
[photo] => /uploads/media/products/product_xyz.jpg
[link] => /product/3-product-xyz/
[sku] => PRODUCT-XYZ
[weight] => 10
[attributes] => Array
(
...
)
)
)
[859d1bb225ba5d16de4d4c23076cfae0] => Array
(
[id] => 3
[qty] => 2
[price] => 30.00
[title] => Product XYZ
[data] => Array
(
[photo] => /uploads/media/products/product_xyz.jpg
[link] => /product/3-product-xyz/
[sku] => PRODUCT-XYZ
[weight] => 15
[attributes] => Array
(
...
)
)
)
)

Related

Array PHP, Check if exist multiple elements to another array

I have a question about this:
I have two array, one is static, and one can be updated by the user...
I would like to check for every id from the static array if exist the id to the other array, and if exsist, do something, if doesn't exist (when finish to check) pass to other ID etc...
now, the arrays are these:
user array (the user unlock 2 achievement):
Array (
[0] => Array (
[data] => Array (
[importance] => 0
[achievement] => Array (
[id] => 644081262362202
[title] => Achievement 2
[type] => game.achievement
[url] => http://www.***.com/achievements/achievement2.html
)
)
[id] => 104693166566570
)
[1] => Array (
[data] => Array (
[importance] => 0
[achievement] => Array (
[id] => 968802826528055
[title] => Achievement 1
[type] => game.achievement
[url] => http://www.***.com/achievements/achievement1.html
)
)
[id] => 104023386633548
)
)
the static Array (have 6 achievement saved):
Array (
[0] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement2
[title] => Achievement 2
[id] => 644081262362202
)
[1] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement3
[title] => Achievement 3
[id] => 912599152147444
)
[2] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement5
[title] => Achievement 5
[id] => 913757345379232
)
[3] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement6
[title] => Achievement 6
[id] => 921989084564878
)
[4] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement1
[title] => Achievement 1
[id] => 968802826528055
)
[5] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement4
[title] => Achievement 4
[id] => 1149671038394021
)
)
now, I use this script to echo the final output like the picture (results is the static array):
if (empty($results)) {
//echo 'noAchievement for the app';
} else {
foreach ($results as $result) {
$totalAchievementsApp .= ' [["' . "0" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";
}
}
now, How I can do to check inside the this script? I know I have to add another if inside the else to check if the ID is = to other ID, but I don't know how, I'm a little bit confused... I would like to check if the id of the static array exist in the other array, and if exsist, do this:
**$totalAchievementsApp .= ' [["' . "1" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";**
Thank you very much :)
If I understand correctly, you want to indicate for each entry in the static array whether its ID exists in the user array.
You can use array_column to generate an array of all IDs in the user array. Then use in_array to check if each static ID exists in that array. Set a value to 1 if its found and 0 if its not found.
For the sake of example, I've generated a new final output array. But you could just add the "found" value to each entry of the the static array.
<?php
$static=array(
array('point'=>50,'title'=>'TITLE 1','id'=>54632),
array('point'=>50,'title'=>'TITLE 2','id'=>54344),
array('point'=>50,'title'=>'TITLE 3','id'=>34225),
array('point'=>50,'title'=>'TITLE 4','id'=>2323245),
array('point'=>50,'title'=>'TITLE 5','id'=>23872445),
);
$user=array(
array('id'=>2323245,'title'=>'TITLE 1','point'=>50),
array('id'=>54344,'title'=>'TITLE 2','point'=>50),
array('id'=>34225,'title'=>'TITLE 3','point'=>50)
);
$final=array();
foreach ($static as $entry) {
$final[]=array(
'found'=>in_array($entry['id'],array_column($user,'id'))?1:0,
'id'=>$entry['id'],
'title'=>$entry['title'],
'point'=>$entry['point']
);
}
echo"<pre>".print_r($final,true)."</pre>";
With your data, the output is:
Array
(
[0] => Array
(
[found] => 0
[id] => 54632
[title] => TITLE 1
[point] => 50
)
[1] => Array
(
[found] => 1
[id] => 54344
[title] => TITLE 2
[point] => 50
)
[2] => Array
(
[found] => 1
[id] => 34225
[title] => TITLE 3
[point] => 50
)
[3] => Array
(
[found] => 1
[id] => 2323245
[title] => TITLE 4
[point] => 50
)
[4] => Array
(
[found] => 0
[id] => 23872445
[title] => TITLE 5
[point] => 50
)
)
EDIT
Given the more complex structure of your actual arrays, I nested several array_column functions to access the deeper "data > achievement > id" keys in your user array:
$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');
See the example below:
// initialize the "static" and "user" arrays
$static=array (
0 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement2',
'title' => 'Achievement 2',
'id' => 644081262362202
),
1 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement3',
'title' => 'Achievement 3',
'id' => 912599152147444
),
2 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement5',
'title' => 'Achievement 5',
'id' => 913757345379232
),
3 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement6',
'title' => 'Achievement 6',
'id' => 921989084564878
),
4 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement1',
'title' => 'Achievement 1',
'id' => 968802826528055
),
5 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement4',
'title' => 'Achievement 4',
'id' => 1149671038394021
)
);
$user=array(
0=>array(
'data' => array(
'importance' => 0,
'achievement' => array (
'id' => 644081262362202,
'title' => 'Achievement 2',
'type' => 'game.achievement',
'url' => 'http://www.***.com/achievements/achievement2.html'
)
),
'id' => 104693166566570
),
1 => array (
'data' => array (
'importance' => 0,
'achievement' => array (
'id' => 968802826528055,
'title' => 'Achievement 1',
'type' => 'game.achievement',
'url' => 'http://www.***.com/achievements/achievement1.html'
)
),
'id' => 104023386633548
)
);
// build array of user achievement IDs
$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');
// generate final array, with "found" values
$final=array();
foreach ($static as $entry) {
$final[]=array(
'found'=>in_array($entry['id'],$user_achvmts)?1:0,
'id'=>$entry['id'],
'title'=>$entry['title'],
'description'=>$entry['description'],
'points'=>$entry['data']['points']
);
}
echo"<pre>".print_r($final,true)."</pre>";
The result is:
Array
(
[0] => Array
(
[found] => 1
[id] => 644081262362202
[title] => Achievement 2
[description] => you unlock the achievement2
[points] => 50
)
[1] => Array
(
[found] => 0
[id] => 912599152147444
[title] => Achievement 3
[description] => you unlock the achievement3
[points] => 50
)
[2] => Array
(
[found] => 0
[id] => 913757345379232
[title] => Achievement 5
[description] => you unlock the achievement5
[points] => 50
)
[3] => Array
(
[found] => 0
[id] => 921989084564878
[title] => Achievement 6
[description] => you unlock the achievement6
[points] => 50
)
[4] => Array
(
[found] => 1
[id] => 968802826528055
[title] => Achievement 1
[description] => you unlock the achievement1
[points] => 50
)
[5] => Array
(
[found] => 0
[id] => 1149671038394021
[title] => Achievement 4
[description] => you unlock the achievement4
[points] => 50
)
)
Note that array_column is only available in PHP >= 5.5.0. For older versions, see the Recommended userland implementation for PHP lower than 5.5.
As an alternative to array_column, you could use array_map to build an array of the user IDs:
$user_achvmts = array_map( function($v) {return $v['data']['achievement']['id'];}, $user);
Or even just iterate through the user array:
$user_achvmts=[];
foreach ($user as $v) { $user_achvmts[]=$v['data']['achievement']['id']; }

Not all PHP Sessions saved

I am encountering this problem. In my PHP code I am setting two sessions: Cart and Visited.
A cart is clear, visited is a function to view the latest visited products.
My cart has this array:
Partnr
Productname
Quantity
Productgroep
Subgroup
Visited has this array:
Partnr
Productname
Description
Productgroup
Subgroup
The session of the cart is stored correctly, but the visited isn't.
This is the code of the cart:
$_SESSION['cart'][] = array('quantity' => $_POST['quantity'],
'partnr' => $_POST['partnr'],
'hoofdgroep' => $_POST['hoofdgroep'],
'subgroep' => $_POST['subgroep'],
'productname' => $_POST['productname']
);
And the code of visited:
$addition = array('productcode' => $csv[1][1],
'productname' => $csv[1][2],
'description' => $csv[1][4],
'hoofdgroep' => $_GET['hoofdgroep'],
'subgroep' => $_GET['subgroep']);
$_SESSION['visited'][] = $addition;
Does anyone know why my visited won't be saved and by cart is saved?
UPDATE:
Session visited after 3 products shown $_SESSION['visited']:
Array
(
[0] => Array
(
[productcode] => AD-HPR-150-CR50
[productname] => SimpleXMLElement Object
(
[0] => Adapterring voor HPR-150
)
[description] => SimpleXMLElement Object
(
[0] => Adaptar
)
[hoofdgroep] => lighting
[subgroep] => accessoires
)
)
My cart session with three products in it $_SESSION['cart']:
Array
(
[0] => Array
(
[quantity] => 1
[partnr] => AD-HPR-150-CR50
[hoofdgroep] => lighting
[subgroep] => accessoires
[productname] => Adapterring voor HPR-150
)
[1] => Array
(
[quantity] => 1
[partnr] => AD-HPR-150-CR50
[hoofdgroep] => lighting
[subgroep] => accessoires
[productname] => Adapterring voor HPR-150
)
[2] => Array
(
[quantity] => 1
[partnr] => HP-CDT1006B-24
[hoofdgroep] => lighting
[subgroep] => diffuselite-dome
[productname] => HighPower 45inch Compact Diffused Tube Light (with inline dimmer 10 turn locking knob) Blue, 24VDC
)
)
UPDATE 2:
And the revised code of visited:
$addition = array('productcode' => $csv[1][1],
'productname' => (string)$csv[1][2],
'description' => (string)$csv[1][4],
'hoofdgroep' => $_GET['hoofdgroep'],
'subgroep' => $_GET['subgroep']);
$_SESSION['visited'][] = $addition;

mongodb $inc a field inside an array

hi i have the follwing array structure
Array
(
[_id] => MongoId Object
(
[$id] => 538978ce8ead0ec1048b456c
)
[cartId] => 98374319ff71dbc3a84b842b7a443cf7
[products] => Array
(
[0] => Array
(
[productId] => 100343
[quantity] => 17
[name] => a
)
[1] => Array
(
[productId] => 100344
[quantity] => 3
[name] => ab
)
[2] => Array
(
[productId] => 100345
[quantity] => 1
[name] => abc
)
)
And i'm having problems to increment the quantity of the products based on the productId
I now use the position but i have no reference on id
$oCartsCollection->update(array('cartId'=>'98374319ff71dbc3a84b842b7a443cf7'), array('$inc' => array('products.0.quantity'=>1)));
What you need to do is add to your query to select and element from your array and then use the positional $ operator in order to match that position:
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products.productId' => 100343
),
array('$inc' => array('products.$.quantity'=>1)));
The "dot" notation method is fine for accessing the productId element in this case. For multiple fields to match use $elemMatch instead
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products' => array(
'$elemMatch' => array(
'productId' => 100343,
'name' => 'a'
)
)
),
array('$inc' => array('products.$.quantity'=>1)));

How To push array elements from one array to another array

Hello people here is the code that i was using initially....
array_push($Parameter_IdArray, $Parameter_Id1, $Parameter_Id2, $Parameter_Id3, $OptParameter_Id);
array_push($Eqt_ParamArray, $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
i had no issues to push array values .... but now $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 are in one more array it is something like this
Array ( [Profile_Id] => 4 [eqt_param] => Array ( [0] => 4.00 [1] => 4.00 [2] => 4.00 ) [Parameter_Id1] => 8 [min_param] => Array ( [0] => 1.00 [1] => 1.00 [2] => 1.00 ) [max_param] => Array ( [0] => 5.00 [1] => 5.00 [2] => 5.00 ) [Wtg_param] => Array ( [0] => 25.00 [1] => 25.00 [2] => 50.00 ) [Parameter_Id2] => 5 [Parameter_Id3] => 1 [Opt_eqt_param] => Array ( [0] => 0.00 ) [OptParameter_Id] => 14 [Opt_wtg] => Array ( [0] => 1.05 ) [operator] => Array ( [0] => M ) [eqt_pay] => 1,574,235 [rec_sal] => 1,485,000 [#] => -6.01 % [Emp_Id] => 490699 [Emp_Process] => Confirm New Pay )
now i need tp push array values $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 to $Eqt_ParamArray how to do that?
If I understand correctly, you've now got an associative array in PHP. On that assumption, I went ahead and reformatted the nightmare for you:
$myarray = array(
"Profile_Id" => 4,
"eqt_param" => array(4.00, 4.00, 4.00),
"Parameter_Id1" => 8,
"min_param" => array (1.00, 1.00, 1.00 ),
"max_param" => array (5.00, 5.00, 5.00 ),
"Wtg_param" => array (25.00, 25.00, 50.00 ),
"Parameter_Id2" => 5,
"Parameter_Id3" => 1,
"Opt_eqt_param" => array (0.00),
"OptParameter_Id" => 14,
"Opt_wtg" => array (1.05),
"operator" => array ("M"),
"eqt_pay" => array(1,574,235),
"rec_sal" => array(1,485,000),
"#" => "-6.01 %",
"Emp_Id" => 490699,
"Emp_Process" => "Confirm New Pay"
);
What this good formatting now makes clear is that you can use array_push directly on the "eqt_param" index:
array_push($myArray["eqt_param"], $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
You may also mean that you want to replace it, which is easy too:
$myArray['eqt_param'] = array($eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
The same principles apply in Javascript, which you have had tagged so maybe you mean that.

How to loop through subarray to see if value is return in string

Here is an example of an array that is output:
Array ( [CART] => Array ( [ITEMS] => Array ( [0] => Array ( [product_id] => 269194 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => 15 Top Hits for Easy Piano [product_code] => HL102668 [product_price] => 14.9900 [original_price] => 14.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) [1] => Array ( [product_id] => 266421 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => Whistle [product_code] => HD245839 [product_price] => 3.9900 [original_price] => 3.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) ) [LAST_UPDATED] => 1349829499 [NUM_ITEMS] => 2 ) [JustAddedProduct] => [CHECKOUT] => Array ( ) )
There is an array for each unique product (in this example there are 2 unique products.) Sometimes there will be just one, sometimes there could be 20 or more unique products.
The value that is important to me is [product_code]. You can see that in the first array, there is [product_code] => HL102668. In the second there is [product_code] => HD245839.
How can I check to see if 'HD' exists in any of the [product_code] values? If it does, I need to return false.
Thank you for your help!
Access your sub array :
$products = $array['CART']['ITEMS'];
Loop through your sub array :
foreach ($products as $product)
Check if HD exists in your product_code, with either simple strstr, or with regex using preg_match (if you are comfortable with it).
if (strstr($product['product_code'], "HD")) {
// Do your actions
}

Categories