如何摆脱重复的价值......
foreach($get_all as $key => $value)
{
$v = 1;
foreach($value as $loc)
{
foreach($loc as $l)
{
$new[] = $l['id'];
}
}
$new = array_unique($new);
$total = count($new);
unset($new);
if($v)
{
$v =0 ;
$body .= "<tr><td>{$value[0][0]['location']}</td><td>{$total}</td></tr>";
continue;
}
}
上面的代码给出了输出:
Koramangala 63
Koramangala 63
Indiranagar 36
Koramangala 63
Indiranagar 36
MG Road 16
Koramangala 63
Indiranagar 36
MG Road 16
BTM 35
但我需要的是:
Koramangala 63
Indiranagar 36
MG Road 16
BTM 35
我需要什么来获得所需的输出?/ ??
答案 0 :(得分:0)
你可以这样做
$printed = array(); // initialize an array to store the printed values
foreach($get_all as $key => $value)
{
$v = 1;
foreach($value as $loc)
{
foreach($loc as $l)
{
$new[] = $l['id'];
}
}
$new = array_unique($new);
$total = count($new);
unset($new);
if($v)
{
$v =0 ;
if (!in_array($value[0][0]['location'], $printed)){ //check if not already printed
$body .= "<tr><td>{$value[0][0]['location']}</td><td>{$total}</td></tr>";
$printed[]=$value[0][0]['location']; //store the printed location to the array
}
continue; //this is not required as it is the last line
}
}
注意:我不知道您使用$v
的原因(可能您只发布了相关部分)。您的数据结构不明确,因此这可能不是最佳(优化)选项。