Php array problems

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.