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

图片转代码服务由CSDN问答提供 功能建议

我有这个数组(我无法改变它):

   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)“aaaaaaaaaaa”
 [0] =>  string(12)“aaaaaaaaaaa”
 [“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)
} 
} 
   
 
 

正如您所看到的,我得到了重复的值,因为数组作为定义的键然后是整数键。

如何创建一个函数,它将从数组中删除整数键和值并返回一个已经“干净”的新数组

结果期望 应该是这样的:

  array(3){
 [0] => 
 array(8){
 [“kampania”] =>  string(6)“dasdas”
 [“300x250”] =>  int(1)
 [“160x600”] =>  int(2)
 [“728x90”] =>  int(3)
} 
 [1] => 
 array(8){
 [“kampania”] =>  string(12)“aaaaaaaaaaa”
 [“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)
} 
} 
   
 
 

谢谢大家! 我真的很抱歉问它,但我已经松了很多时间试图自己解决它

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    已采纳该答案
    打赏 评论
  • dtwr2012 2015-11-26 10:33

    Loop threw the array and if the key(index) is numeric then remove that item from the array.

    foreach($level1_array as &$arr){
    
      foreach ($arr as $key => $value) {
        if (is_int($key)) {
          unset($arr[$key]);
         }
       }
    }
    

    Example

    <?php 
    $level1_array = array(
    
        array(
                "smith",
                "name" => "smith",
                "20",
                "age"=>20
            ),
        array(
                "smith",
                "name" => "smith",
                "20",
                "age"=>20
            ),
         array(
                "smith",
                "name" => "smith",
                "20",
                "age"=>20
            ),
    );
    
    foreach($level1_array as &$arr){
    
     foreach ($arr as $key => $value) {
       if (is_int($key)) {
         unset($arr[$key]);
        }
      }
    }
    
    var_dump($level1_array);
    ?>
    
    打赏 评论
  • dsk920417 2015-11-26 10:59

    You can try this code:

    //$data = $yourData;
    $newData = array();
    foreach($data as $v){
        array_push($newData,array_flip($v));
    }
    
    打赏 评论

相关推荐 更多相似问题