PHP 如何计算foreach循环中的相同单词
PHP How to count same words in foreach loop
我的 foreach 循环中有这个结果:
Tonsilitis
Tonsilitis
Laryngitis
Rhinosinusitis Akut
Rhinosinusitis Akut
Rhinosinusitis Akut
Common Cold
Common Cold
Common Cold
Rhinitis Alergi
这是我的脚本:
foreach ($results as $data) :
$final = $data->nama_diagnosis . '<br>';
echo $final;
endforeach;
我的问题是,我如何计算循环中或循环外的同一个词。我可以这样做吗?请给我解决方案。结果我想这样计算它们:
Tonsilitis = 2
Laryngitis = 1
Rhinosinusitis Akut = 3
Common Cold = 3
Rhinitis Alergi = 1
或者我可以过滤同一个词,这样我只得到最多的词,比如急性鼻窦炎和普通感冒。请帮我。
谢谢
您可以尝试这样的操作,使用 foreach
循环遍历数组并使用带有 isset
的三元运算符来安全地分配和递增每次出现:
$count = array();
foreach ($results as $result)
isset($count[$data]) ? $count[$data]++ : $count[$data] = 1;
Example
试试 -
$counts = array();
foreach ($results as $data) :
$final = $data->nama_diagnosis . '<br>';
if (array_key_exists($data->nama_diagnosis, $counts)) {
$counts[$data->nama_diagnosis] += 1;
} else {
$count[$data->nama_diagnosis] = 1;
}
endforeach;
foreach ($counts as $key => $val) {
echo $key.' = '.$val;
}
在 foreach 循环中将单词和它们的计数保存到数组中,然后进行另一个循环并写入数量。
<?php
$results = array(
array('nama_diagnosis' => 'Tonsilitis'),
array('nama_diagnosis' => 'Tonsilitis'),
array('nama_diagnosis' => 'Laryngitis'),
array('nama_diagnosis' => 'Rhinosinusitis Akut'),
array('nama_diagnosis' => 'Rhinosinusitis Akut'),
array('nama_diagnosis' => 'Rhinosinusitis Akut'),
array('nama_diagnosis' => 'Common Cold'),
array('nama_diagnosis' => 'Common Cold'),
array('nama_diagnosis' => 'Common Cold'),
array('nama_diagnosis' => 'Rhinitis Alergi')
);
$res = array();
foreach ($results as $words) { // changed $word to $words
foreach ($words as $word) { // this foreach added
if (isset($res[$word])) {
$res[$word] += 1;
} else {
$res[$word] = 1;
}
} // end of nested foreach which was added
}
foreach ($res as $word => $count) {
echo $word . ' (' . $count . ')<br>';
}
/*
output:
Tonsilitis (2)
Laryngitis (1)
Rhinosinusitis Akut (3)
Common Cold (3)
Rhinitis Alergi (1)
*/
?>
这应该适合你:
<?php
$results = array("Tonsilitis", "Tonsilitis", "Laryngitis", "Rhinosinusitis Akut", "Rhinosinusitis Akut", "Rhinosinusitis Akut", "Common Cold", "Common Cold", "Common Cold", "Rhinitis Alergi");
$counter = array();
foreach ($results as $data):
if(in_array($data->nama_diagnosis, array_flip($counter)))
$counter[$data->nama_diagnosis]++;
else
$counter[$data->nama_diagnosis] = 1;
endforeach;
foreach ($counter as $key => $data)
echo $key . " = " . $data . "<br />";
?>
输出:
Tonsilitis = 2
Laryngitis = 1
Rhinosinusitis Akut = 3
Common Cold = 3
Rhinitis Alergi = 1
我的 foreach 循环中有这个结果:
Tonsilitis
Tonsilitis
Laryngitis
Rhinosinusitis Akut
Rhinosinusitis Akut
Rhinosinusitis Akut
Common Cold
Common Cold
Common Cold
Rhinitis Alergi
这是我的脚本:
foreach ($results as $data) :
$final = $data->nama_diagnosis . '<br>';
echo $final;
endforeach;
我的问题是,我如何计算循环中或循环外的同一个词。我可以这样做吗?请给我解决方案。结果我想这样计算它们:
Tonsilitis = 2
Laryngitis = 1
Rhinosinusitis Akut = 3
Common Cold = 3
Rhinitis Alergi = 1
或者我可以过滤同一个词,这样我只得到最多的词,比如急性鼻窦炎和普通感冒。请帮我。 谢谢
您可以尝试这样的操作,使用 foreach
循环遍历数组并使用带有 isset
的三元运算符来安全地分配和递增每次出现:
$count = array();
foreach ($results as $result)
isset($count[$data]) ? $count[$data]++ : $count[$data] = 1;
Example
试试 -
$counts = array();
foreach ($results as $data) :
$final = $data->nama_diagnosis . '<br>';
if (array_key_exists($data->nama_diagnosis, $counts)) {
$counts[$data->nama_diagnosis] += 1;
} else {
$count[$data->nama_diagnosis] = 1;
}
endforeach;
foreach ($counts as $key => $val) {
echo $key.' = '.$val;
}
在 foreach 循环中将单词和它们的计数保存到数组中,然后进行另一个循环并写入数量。
<?php
$results = array(
array('nama_diagnosis' => 'Tonsilitis'),
array('nama_diagnosis' => 'Tonsilitis'),
array('nama_diagnosis' => 'Laryngitis'),
array('nama_diagnosis' => 'Rhinosinusitis Akut'),
array('nama_diagnosis' => 'Rhinosinusitis Akut'),
array('nama_diagnosis' => 'Rhinosinusitis Akut'),
array('nama_diagnosis' => 'Common Cold'),
array('nama_diagnosis' => 'Common Cold'),
array('nama_diagnosis' => 'Common Cold'),
array('nama_diagnosis' => 'Rhinitis Alergi')
);
$res = array();
foreach ($results as $words) { // changed $word to $words
foreach ($words as $word) { // this foreach added
if (isset($res[$word])) {
$res[$word] += 1;
} else {
$res[$word] = 1;
}
} // end of nested foreach which was added
}
foreach ($res as $word => $count) {
echo $word . ' (' . $count . ')<br>';
}
/*
output:
Tonsilitis (2)
Laryngitis (1)
Rhinosinusitis Akut (3)
Common Cold (3)
Rhinitis Alergi (1)
*/
?>
这应该适合你:
<?php
$results = array("Tonsilitis", "Tonsilitis", "Laryngitis", "Rhinosinusitis Akut", "Rhinosinusitis Akut", "Rhinosinusitis Akut", "Common Cold", "Common Cold", "Common Cold", "Rhinitis Alergi");
$counter = array();
foreach ($results as $data):
if(in_array($data->nama_diagnosis, array_flip($counter)))
$counter[$data->nama_diagnosis]++;
else
$counter[$data->nama_diagnosis] = 1;
endforeach;
foreach ($counter as $key => $data)
echo $key . " = " . $data . "<br />";
?>
输出:
Tonsilitis = 2
Laryngitis = 1
Rhinosinusitis Akut = 3
Common Cold = 3
Rhinitis Alergi = 1