-4

我在 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,我可以在每种情况下获得产品和编号,但我如何才能在其他模式下获得,例如与 print 或 echo 的正常功能一起使用,我尝试最终获得数据显示如下:

Product 1 (6)
Product 2 (3)
Product 3 (4)

感谢和问候

4

1 回答 1

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/>";

}




?>
于 2012-12-13T13:21:04.590 回答