【问题标题】:Eliminate duplicate key with same value from associative array in php从php中的关联数组中消除具有相同值的重复键
【发布时间】:2015-06-05 09:51:17
【问题描述】:
Array
(
    [0] => Zimbabwe
    [country] => Zimbabwe
    [1] => 2
    [counts] => 2
)
Array
(
    [0] => Tunisia
    [country] => Tunisia
    [1] => 6
    [counts] => 6
)   
I want output like below:  
Array
(
    [country] => Zimbabwe 
    [counts] => 2
)  
Array
(
    [country] => Tunisia   
    [counts] => 6
)

【问题讨论】:

  • 当您从数据库中检索数据时(您可能正在使用 mysql_fetch_array()mysqli_fetch_array() 调用,不是吗),告诉 fetch 返回一个关联数组,而不是同时返回一个跨度>
  • 我正在使用 while($rowCountry = mssql_fetch_array($resultCountryCount)){ print_r($rowCountry);} .谢谢你的帮助
  • 如果您使用mysql_fetch_array@MarkBaker 的建议比我的要好得多。 ;)
  • @MarkBaker 100% 正确。大眼睛。

标签: php arrays multidimensional-array associative-array


【解决方案1】:

尝试使用

从您的数据库中获取它
while($rowCountry = mssql_fetch_array($resultCountryCount), MSSQL_ASSOC){
    print_r($rowCountry);
}

while($rowCountry = mssql_fetch_assoc($resultCountryCount)){
    print_r($rowCountry);
}

【讨论】:

    【解决方案2】:

    最好使用@mark,否则你也可以试试这个

      $a = Array
        (
            0 => 'Zimbabwe',
            'country' => 'Zimbabwe',
            1=> 2,
            'counts' => 2,
        );
    
        function myfun($array){
            $check= array();
            foreach($array as $k => $v){
                if(in_array($v,$check)){
                    unset($array[$k]);
                }   
                array_push($check, $v);
            }
            return $array;
        }
        $a = myfun($a);
        echo '<pre>';
        print_r($a);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-30
      • 2018-07-01
      • 2010-09-19
      • 2021-10-26
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      相关资源
      最近更新 更多