[1] => 天津 [2] => 北京 )  Array ( [0] => [1] => 北京 [2] => 上"/> [1] => 天津 [2] => 北京 )  Array ( [0] => [1] => 北京 [2] => 上">

Heim  >  Artikel  >  Backend-Entwicklung  >  php 数组有关问题

php 数组有关问题

WBOY
WBOYOriginal
2016-06-13 10:33:40705Durchsuche

php 数组问题
$abc是从数据库中循环出来的
$pieces = explode("|", $abc);
print_r($pieces);
打印结果是
Array ( [0] => [1] => 天津 [2] => 北京 ) 
Array ( [0] => [1] => 北京 [2] => 上海 [3] => 河北 ) 
Array ( [0] => [1] => 天津 [2] => 河北 [3] => 北京 )
我想知道 天津、河北、北京、上海,各出现次数是多少。
好像不能用这个array_intersect()

------解决方案--------------------
先把数组合并 在用 array_count_values 

PHP code
<?php $array = array(1, "hello", 1, "world", "hello");print_r(array_count_values ($array));?><br><font color="#e78608">------解决方案--------------------</font><br>$array_count = array();//不能放你循环里,初始化1次<br>foreach($pieces as $v){<br>   if(!empty($v)){<br>       if(empty($array_count[$v])){<br>           $array_count[$v] = 1;<br>       }else{<br>           $array_count[$v] += 1;<br>       }<br>   }<br>}<br><br>$pieces 是每次循环打断出来的。<div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn