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 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程