在虚拟数据库中,我有一个用户ID列表和一个产品ID列表(属于他们购买的商品)。
数据是这样的:
1:54,3:54,3:88,7:234,4:344,1:54,76:12,1:54
代表: UserID : ProductID
从以上数据中您可以看到:
用户ID 1 已进行3次销售
用户ID 3 已进行2次销售
用户ID 7 已进行1次促销
用户ID 4 已进行1次促销
用户ID 76 已进行1次促销
我很难得到结果,所以我可以在表格中显示它们:
User ID | # Sales
____________________
1 3
3 2
7 1
4 1
76 1
____________________
Unique Users (5)
Total Sales (8)
目前我有
$data = "1:54,3:54,3:88,7:234,4:344,1:54,76:12,1:54";
$data = explode(",", $data);
$result = array();
foreach ($data as $value) {
$d_array = explode(":", $value);
foreach ($d_array as $key => $value) {
echo "<br/> Use: " .$key . "Item: ". $value;
}
}
我得到的是:
User:0 Item: 1
User:1 Item: 54
User:0 Item: 3
User:1 Item: 54
/// etc...
如何获得独特的用户数量和总销售额?
答案 0 :(得分:1)
试试这个:
$data = "1:54,3:54,3:88,7:234,4:344,1:54,76:12,1:54";
$data = explode(",", $data);
$result = array();
foreach ($data as $value) {
$d_array = explode(":", $value);
if(isset($result[$d_array[0]]))
$result[$d_array[0]] +=1;
else
$result[$d_array[0]] = 1;
}
$user_unique = count($result);
$total = count($data);
var_dump($result);
var_dump($total);
var_dump($user_unique);
结果
array (size=5)
1 => int 3
3 => int 2
7 => int 1
4 => int 1
76 => int 1
int 8
int 5
答案 1 :(得分:0)
试试这个:
$data = "1:54,3:54,3:88,7:234,4:344,1:54,76:12,1:54";
$data = explode(",", $data);
$result = array();
foreach ($data as $value) {
$d_array = explode(":", $value);
if (count($d_array) == 2)
echo "<br/> Use: " .$d_array[0] . "Item: ". $d_array[1];
}
爆炸功能将字符串扩展为数组项而不是名称 - 值对。
答案 2 :(得分:0)
我能想到的最简单的方法就是在打印时计算用户数。
像这样:
$data = "1:54,3:54,3:88,7:234,4:344,1:54,76:12,1:54";
$data = explode(",", $data);
$totalSalesNr = = $totalSalesAmnt = 0;
$result = $users = array();
foreach ($data as $value) {
$d_array = array();
parse_str(str_replace(':', '=', $value), $d_array);
foreach ($d_array as $key => $value) {
$totalSalesNr++; $totalSalesAmnt += $value;
if(!in_array($key, $users)) $users[] = $key;
echo "<br/> User: " .$key . "Item: ". $value;
}
}
echo "<br />Total Users: ". count($users) . " Total Sales: $totalSalesNr (Amount: $totalSalesAmnt)";
答案 3 :(得分:0)
所以你想得到
唯一的用户数量和总销售额
然后你可以像这样做魔术:
$http-request
答案 4 :(得分:0)
在ForEach循环中,我们可以满足您的要求。
$data = "1:54,3:54,3:88,7:234,4:344,1:54,76:12,1:54";
$data = explode(",", $data);
$dataItem = array();
foreach ($dataItem as $item) {
$uid = explode(":", $item);
array_push($dataItem, $uid[0]);
}
foreach (array_unique($dataItem) as $item) {
$count = 0;
foreach ($dataItem as $subitem) {
if($item == $subitem)
$count++;
}
echo "<br/> User ID: " .$item . "Sales: ". $count;
}
答案 5 :(得分:0)
我认为你正在寻找这个回答,
通过这种方式,您将获得每个用户拥有的所有项目 结果数组上的键将是用户,值将是每个用户拥有的项目。foreach ($data as $value) {
$d_array = explode(":", $value);
if (isset($result[trim($d_array[0])] ) ){
$result[trim($d_array[0])] .= trim($d_array[1]) . ' | ';
}else{
$result[trim($d_array[0])] ='Items: ' .$d_array[1] . ' | ';
}
}
print_r($result);
总销售额,您可以简单地
$data = '1:54,3:54,3:88,7:234,4:344,1:54,76:12,1:54';
$data = explode(",", $data);
$count = count($data); // total sales
print_r($count) ;
输出
销售
Array
(
[1] => Items: 54 | 54 | 54 |
[3] => Items: 54 | 88 |
[7] => Items: 234
[4] => Items: 344
[76] => Items: 12
)
Total Sales
8
希望有所帮助