我在txt文件中有这个商店产品的数据:
product 1
product 1
product 2
product 2
product 2
product 1
product 1
product 3
product 3
product 3
product 1
product 1
product 3
product 1
好的,人们将产品添加到购物卡中,这些产品一般都插入txt
要恢复数据,请使用下一个脚本:
<?php
$file_data=file("products.txt");
for ($i=0;$i<sizeof($file_data);$i++)
{
/// Create array of groups
$global_products[]=$file_data[$i];
}
/// Now i want get the products and the number in each case for example of the product 1 i have 6 for the product 2 i have 3 and for the product 3 i have 4 , ok for get this i can use thi function
$prr=array_count_values($global_productos);
print_r (object2array($prr),true);
?>
如果我使用print_r,我可以获得每种情况下的产品和编号,但我可以如何进入其他模式,例如用于打印或回波的正常功能,我尝试最后得到数据显示为:
Product 1 (6)
Product 2 (3)
Product 3 (4)
感谢问候
答案 0 :(得分:0)
使用以下代码
<?php
$file_data=file("output_rrr.txt");
for ($i=0;$i<sizeof($file_data);$i++)
{
/// Create array of groups
$global_products[]=$file_data[$i];
}
/// Now i want get the products and the number in each case for example of the product 1 i have 6 for the product 2 i have 3 and for the product 3 i have 4 , ok for get this i can use thi function
$prr=array_count_values($global_products);
foreach($prr as $key=>$value)
{
echo trim($key).": ".$value."<br/>";
}
?>