Flezria Posted October 29, 2012 Share Posted October 29, 2012 Hi guys! I have a list of ID's where i want to count how many of them there is equally to each other. My list is like this: 174,174,226,227,226,226,226 Anyone can tell me how to do this? I've the following code at the moment: $cart = $_SESSION['cart']; $items = explode(",",$cart); echo $cart; $i = 0; $count = count($items); $antal = array(); while($i < $count) { { $i++; // Some code in here } } Thanks a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/ Share on other sites More sharing options...
requinix Posted October 29, 2012 Share Posted October 29, 2012 Try array_count_values. Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388505 Share on other sites More sharing options...
Flezria Posted October 29, 2012 Author Share Posted October 29, 2012 Ah, forgot to say i've already tried that.. :-( Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388506 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2012 Share Posted October 29, 2012 If you change the definition of your cart to be an array - $_SESSION['cart'][id] = n; // where n is the quantity of that id in the cart, all your code will be greatly simplified. The sample cart you posted would look like - $_SESSION['cart'][174] = 2 $_SESSION['cart'][226] = 4 $_SESSION['cart'][227] = 1 Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388509 Share on other sites More sharing options...
requinix Posted October 29, 2012 Share Posted October 29, 2012 Ah, forgot to say i've already tried that.. :-( How so? Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388510 Share on other sites More sharing options...
Flezria Posted October 29, 2012 Author Share Posted October 29, 2012 If you change the definition of your cart to be an array - $_SESSION['cart'][id] = n; // where n is the quantity of that id in the cart, all your code will be greatly simplified. The sample cart you posted would look like - $_SESSION['cart'][174] = 2 $_SESSION['cart'][226] = 4 $_SESSION['cart'][227] = 1 I don't quite understand what you mean. I want to save the output in a new array, so i can use it to post later on, would it look like this? $cart = $_SESSION['cart']; $cart = array(); $items = explode(",",$cart); $i = 0; $count = count($items); $antal = array(); while($i < $count) { { $antal[$i] = array_count_values($cart) echo $antal[$i]; //Just to check if it works $i++; } } Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388512 Share on other sites More sharing options...
Flezria Posted October 29, 2012 Author Share Posted October 29, 2012 How so? I tried to put it into array_count_values and it told me it was a string, instead of an array. Right now i'm trying to compile the numbers into an array, so i can do array_count_values. I have this right now, but it doesn't seem to quite do the job: $items = explode(',',$_SESSION['cart']); $cart = $_SESSION['cart']; $i = 0; $count = count($items); while($i < $count) { $cart = array($i, "$items[$i]"); $i++; } $antal = array(); while($i < $count) { { $antal[$i] = array_count_values($cart); echo $antal[$i]; $i++; } } Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388515 Share on other sites More sharing options...
Barand Posted October 29, 2012 Share Posted October 29, 2012 <?php $arr = array(174,174,226,227,226,226,226) ; $count = array_count_values($arr); echo '<pre>'.print_r($count, 1).'</pre>'; ?> output: Array ( [174] => 2 [226] => 4 [227] => 1 ) In what way did array_count_values() not tell you what you wanted to know? Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388517 Share on other sites More sharing options...
Flezria Posted October 29, 2012 Author Share Posted October 29, 2012 <?php $arr = array(174,174,226,227,226,226,226) ; $count = array_count_values($arr); echo '<pre>'.print_r($count, 1).'</pre>'; ?> output: Array ( [174] => 2 [226] => 4 [227] => 1 ) In what way did array_count_values() not tell you what you wanted to know? The problem is, those id's are something i get from whatever my customer picked as an order, they're not already an array, so i have to "convert" them somehow, in order to use the array_count_values() function, my code is this, but it doesn't seem to work: $items = explode(',',$_SESSION['cart']); $cart = array(); $i = 0; $count = count($items); while($i < $count) { array_push($cart, $items[$i]); $i++; } $antal = array(); $arraycount = array_count_values($cart); echo '<pre>'.print_r($arraycount, 1).'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388527 Share on other sites More sharing options...
Jessica Posted October 29, 2012 Share Posted October 29, 2012 Do you know what explode does? Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388531 Share on other sites More sharing options...
Flezria Posted October 29, 2012 Author Share Posted October 29, 2012 Do you know what explode does? I'm pretty sure it "splits" the lines, where the "," is, so i get raw numbers. Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388533 Share on other sites More sharing options...
kicken Posted October 29, 2012 Share Posted October 29, 2012 I'm pretty sure it "splits" the lines, where the "," is, so i get raw numbers. Yes, and it gives you back an array which you could then pass into array_count_values. $values = explode(',', $_SESSION['cart']); $count = array_count_values($values); print_r($count); Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388534 Share on other sites More sharing options...
Flezria Posted October 29, 2012 Author Share Posted October 29, 2012 Yes, and it gives you back an array which you could then pass into array_count_values. $values = explode(',', $_SESSION['cart']); $count = array_count_values($values); print_r($count); aaah, thanks a lot! It seems to give the right print, but i doesn't allow me to use like the first number only? fx. I have 100,100,100,101,102 that means i have 3 of the same product, then i want to print the next to the order, so it tells me the customer bought 3 of that product? I tried $count[0], but doesn't seem to do the trick? Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388539 Share on other sites More sharing options...
Jessica Posted October 29, 2012 Share Posted October 29, 2012 If you change the definition of your cart to be an array - $_SESSION['cart'][id] = n; // where n is the quantity of that id in the cart, all your code will be greatly simplified. The sample cart you posted would look like - $_SESSION['cart'][174] = 2 $_SESSION['cart'][226] = 4 $_SESSION['cart'][227] = 1 Did you see this response? Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388540 Share on other sites More sharing options...
Flezria Posted October 29, 2012 Author Share Posted October 29, 2012 Did you see this response? Thanks a lot, that fixed my problem! Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388544 Share on other sites More sharing options...
Jessica Posted October 29, 2012 Share Posted October 29, 2012 (edited) I can only assume you're not being sarcastic, and say you're welcome :-P (on behalf of PFMaBiSmAd of course) Edited October 29, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/#findComment-1388546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.