Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/270042-help-compare-numbers-in-a-list/
Share on other sites

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

 

 

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++;
}
}

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++;
}
}

<?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?

<?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>';

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);

 

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?

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?

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.