dongqiang2358 2015-11-26 10:28
浏览 39
已采纳

从多维数组PHP中删除Int键和值

I have this array (i can't change it) :

   array(3) {   
[0]=>
  array(8) {
    ["kampania"]=> string(6) "dasdas"
    [0]=> string(6) "dasdas"
    ["300x250"]=> int(1)
    [1]=> int(1)
    ["160x600"]=> int(2)
    [2]=> int(2)
    ["728x90"]=> int(3)
    [3]=> int(3)
  }
  [1]=>
  array(8) {
    ["kampania"]=> string(12) "aaaaaaaaaaaa"
    [0]=> string(12) "aaaaaaaaaaaa"
    ["300x250"]=> int(4)
    [1]=> int(4)
    ["160x600"]=> int(5)
    [2]=> int(5)
    ["728x90"]=> int(6)
    [3]=> int(6)
  }
  [2]=>
  array(8) {
    ["kampania"]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
    [0]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
    ["300x250"]=> int(7)
    [1]=> int(7)
    ["160x600"]=> int(8)
    [2]=> int(8)
    ["728x90"]=> int(9)
    [3]=> int(9)
  }
}

As you can see, I got repeated values, because the array as the defined key and then an integer key.

How can I create a function which will remove the integer key and value from array and return a new array already "clean"

the result expect should be like this:

array(3) {
    [0]=>
      array(8) {
        ["kampania"]=> string(6) "dasdas"
        ["300x250"]=>  int(1)
        ["160x600"]=>  int(2)
        ["728x90"]=>   int(3)
      }
      [1]=>
      array(8) {
        ["kampania"]=> string(12) "aaaaaaaaaaaa"
        ["300x250"]=>  int(4)
        ["160x600"]=>  int(5)
        ["728x90"]=>   int(6) 
      }
      [2]=>
      array(8) {
        ["kampania"]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
        ["300x250"]=> int(7)  
        ["160x600"]=> int(8) 
        ["728x90"]=>  int(9) 
      }
    }

Thanks Guys! I'm really sorry for ask it , but I loose already so much time trying to fix it by myself

  • 写回答

3条回答 默认 最新

  • doucigua0449 2015-11-26 10:44
    关注
    function unset_num_keys($array)
    {
        $array_out = array();
        foreach($array AS $k => $v)
        {
            if(is_array($v))                            //value is an array, so clean it
            {
                $array_out[$k] = unset_num_keys($v);    //clean "child" arrays
            }
            elseif(!is_numeric($k))
            {
                $array_out[$k] = $v;                    // key is "safe"
            }
        }
        return $array_out;
    }
    

    Then

    $clean_array = unset_num_keys($old_array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?