if I have an array of two arrays
如果我有两个数组的数组
D[0] = array ("I", "want", "to", "make", "cake", "and", "make", "juice")
D[1] = array ("Sister", "want", "to", "takes", "the", "cake", "that", "i", "made")
how to count the occurrences of words that are in both arrays?
D [0] = array(“I”,“want”,“to”,“make”,“cake”,“and”,“make”,“juice”)D [1] = array(“Sister”, “want”,“to”,“take”,“the”,“cake”,“that”,“i”,“made”)如何计算两个数组中单词的出现次数?
eg output:
word | array[0] | array[1]
I : 1 | 1
want : 1 | 1
to : 1 | 1
make : 2 | 0
cake : 1 | 1
and : 1 | 0
juice : 1 | 0
sister : 0 | 1
takes : 0 | 1
the : 0 |1
that : 0 | 1
made : 0 | 1
例如输出:word | array [0] | array [1] I:1 | 1想要:1 | 1至:1 | 1制作:2 | 0蛋糕:1 | 1和:1 | 0果汁:1 | 0妹妹:0 | 1取:0 | 1:0 | 1表示:0 | 1作出:0 | 1
2 个解决方案
#1
1
This solution builds an array with allwords, which is later used for iteration of the two lookup arrays $d[0] and $d1. array_unique(array_merge()) to delete duplicate "make" for instance.
该解决方案使用allwords构建一个数组,稍后用于迭代两个查找数组$ d [0]和$ d1。 array_unique(array_merge())例如删除重复的“make”。
The array_count_values() is used for the counting of values.
array_count_values()用于计数值。
Finally, for displaying the table, the allwords array is as iterator.
最后,为了显示表,allwords数组是迭代器。
For each word a new row with id
, word
, calc from array1
, calc from array2
.
对于每个单词,一个新行,其中包含来自array1的id,word,calc,来自array2的calc。
Long story, short. Here's the
长话短说。这是
PHP
PHP
<?php
$d = array();
$d[0] = array("I", "want", "to", "make", "cake", "and", "make", "juice");
$d[1] = array("Sister", "want", "to", "takes", "the", "cake", "that", "i", "made");
$allwords = array_unique(array_merge($d[0], $d[1]));
echo '<table>';
echo '<thead><th>Word ID</th><th>Word</th><th>Array 1</th><th>Array 2</th></thead>';
$array1 = array_count_values($d[0]);
$array2 = array_count_values($d[1]);
foreach($allwords as $id => $word) {
echo '<tr><td>'. $id . '</td><td>' . $word . '</td>';
if(isset($array1[$word])) {
echo '<td>' . $array1[$word] . '</td>';
} else {
echo '<td>0</td>';
}
if(isset($array2[$word])) {
echo '<td>' . $array2[$word] . '</td>';
} else {
echo '<td>0</td>';
}
}
echo '</table>';
Result
结果
#2
0
You can just use array_count_values()
:
你可以使用array_count_values():
$count[0] = array_count_values($D[0]); //Assuming you meant to have D as a variable in your question
$count[1] = array_count_values($D[1]);
The key is the word and the value is the number.
关键是单词,值是数字。
#1
1
This solution builds an array with allwords, which is later used for iteration of the two lookup arrays $d[0] and $d1. array_unique(array_merge()) to delete duplicate "make" for instance.
该解决方案使用allwords构建一个数组,稍后用于迭代两个查找数组$ d [0]和$ d1。 array_unique(array_merge())例如删除重复的“make”。
The array_count_values() is used for the counting of values.
array_count_values()用于计数值。
Finally, for displaying the table, the allwords array is as iterator.
最后,为了显示表,allwords数组是迭代器。
For each word a new row with id
, word
, calc from array1
, calc from array2
.
对于每个单词,一个新行,其中包含来自array1的id,word,calc,来自array2的calc。
Long story, short. Here's the
长话短说。这是
PHP
PHP
<?php
$d = array();
$d[0] = array("I", "want", "to", "make", "cake", "and", "make", "juice");
$d[1] = array("Sister", "want", "to", "takes", "the", "cake", "that", "i", "made");
$allwords = array_unique(array_merge($d[0], $d[1]));
echo '<table>';
echo '<thead><th>Word ID</th><th>Word</th><th>Array 1</th><th>Array 2</th></thead>';
$array1 = array_count_values($d[0]);
$array2 = array_count_values($d[1]);
foreach($allwords as $id => $word) {
echo '<tr><td>'. $id . '</td><td>' . $word . '</td>';
if(isset($array1[$word])) {
echo '<td>' . $array1[$word] . '</td>';
} else {
echo '<td>0</td>';
}
if(isset($array2[$word])) {
echo '<td>' . $array2[$word] . '</td>';
} else {
echo '<td>0</td>';
}
}
echo '</table>';
Result
结果
#2
0
You can just use array_count_values()
:
你可以使用array_count_values():
$count[0] = array_count_values($D[0]); //Assuming you meant to have D as a variable in your question
$count[1] = array_count_values($D[1]);
The key is the word and the value is the number.
关键是单词,值是数字。