Imagine we have an array of students:
Students: Ben Sam …
Every student has a group of books at home:
Books:
- Sam A B C D
- Ben D E F
- Ryan A D G D
So in PHP, the $students data structure looks like this:
Array
(
[0] => Array
(
[name] => Joe Smith
[books] => Array
(
[0] => A
[1] => B
[2] => C
)
)
)
We want to count how many times A has been repeated and who owns A B and C etc.
One way to do it in php is to do this:
if (sizeof($potential_entries) > 0) :
for ($j = 0, $potentialsize = sizeof($potential_entries); $j < $potentialsize; ++$j) {
for ($k = 0, $listsize = sizeof($student_book); $k < $listsize; ++$k) {
if ($student_book[$k] == $potential_entry[$k]) :
$match = 1;
$student_book[$k]['like_count']++;
endif;
}
}
This is not very efficient we rather want a map structure or a hashtable, Does php has those structures like perl? or do we have to manually build them.
Edit:
I can see in the PHP that we have associative array? Can you please give an example of that could not find it in the documentation.
for sam:
var_dump($potential_entry):
[0]=>array(2) { ["name"]=> string(1) "A" ["id"]=> string(11) "1348"}
[1]=>array(2) { ["name"]=> string(1) "B" ["id"]=> string(11) "1483"}
[2]=>array(2) { ["name"]=> string(1) "C" ["id"]=> string(11) "13"}
[3]=>array(2) { ["name"]=> string(1) "D" ["id"]=> string(11) "1174"}
That is for Sam, for the rest we have the same structure. so sam is array and books is an array and sam has an array of books.
In this example
- D count=3 sam ben ryan
- A count=2 sam ryan
- etc…
If you have $students as an array, and each entry in this array has an array of books which is called books
Then after this, any $books entry whose count is > 1 is a duplicate, i.e.