Php array problem $ abc is a loop from the database $ piecesexplode (& quot; | & quot;, $ abc); print_r ($ pieces ); the output is Array ([0] & gt; [1] & gt; Tianjin [2] & gt; Beijing) & nbsp; Array ([0] & gt; [1] & gt; Beijing [2] & gt; php array problems
$ Abc is recycled from the database.
$ Pieces = explode ("|", $ abc );
Print_r ($ pieces );
The printed result is
Array ([0] => [1] => Tianjin [2] => Beijing)
Array ([0] => [1] => Beijing [2] => Shanghai [3] => Hebei)
Array ([0] => [1] => Tianjin [2] => Hebei [3] => Beijing)
I want to know the number of occurrences in Tianjin, Hebei, Beijing, and Shanghai.
It seems that this array_intersect () cannot be used ()
------ Solution --------------------
First, the array is merged with array_count_values.
PHP code
------ Solution --------------------
$ Array_count = array (); // it cannot be placed in your loop and is initialized once.
Foreach ($ pieces as $ v ){
If (! Empty ($ v )){
If (empty ($ array_count [$ v]) {
$ Array_count [$ v] = 1;
} Else {
$ Array_count [$ v] + = 1;
}
}
}
$ Pieces is interrupted every cycle.