My PHP Script
$file_carrito=file("carrito.dat");
$x=0;
for($i=0;$i<sizeof($file_carrito);$i++)
{
$array_products_1[]=$file_carrito[$i];
$x++;
}
echo (array_count_values($array_products_1));
In this simple file .dat , store all products the user add , and count in each case total of products for each id and case
For example i can have into the file 5 ids for one product and other 4 for other product id , finally , this must count products with te same id stored into de dat file
For example : 5 pencils , 3 chairs , 2 tables , etc .....
The problem it´s no works : echo (array_count_values($array_productos_1));
content of carrito.dat :
p1
p1
p1
p3
p3
p3
p2
p2
p2
p1
p1
With this function of array i want get :
Product p1 ---- (5)
Product p2 ----- (3) , etc
Thank´s
count your values like this
$array = array('x', 'x', 'x', 'x', 'y', 'y');
$counts = array();
foreach($array as $value) {
$counts[$value] = array_key_exists($value, $counts) ? $counts[$value]+1 : 1;
}
print_r($counts);
in your case. the array fill with
$array = explode("
", file_get_contents(...));
The easy way:
$data = file('carrito.dat');
print_r(array_count_values($data));
It will show something like the below:
Array
(
[p1] => 5
[p3] => 3
[p2] => 3
)