2012-12-13 33 views
-4

我在txt文件中這一數據爲店鋪的產品:PHP數組數沒有工作我

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我可以在各種情況下的產品和數量,但我怎麼可以在O得到療法模式,例如用於打印或回聲功能正常使用,我嘗試得到最後的數據顯示,因爲這:

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

Thank's問候

+0

循環訪問'$ prr'數組並按照您喜歡的方式打印每個條目。 – alexis

+0

我想操作要計算文件中的每個產品的出價次數 – luchosrock

+0

您能告訴我我該如何做到這一點,可以舉例,謝謝! – user1860536

回答

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

} 




?> 
代碼
+1

Thank's !!!偉大的網站獲得幫助,最好的問候! – user1860536

+0

當然可以! , 謝謝 – user1860536