0

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

4
  • 2
    Why can't you change it? Surely Isn't it your code that's doing the database query as well? Commented Nov 26, 2015 at 10:36
  • 1
    For the record, I assume this is caused by using something like mysql_fetch_array instead of mysql_fetch_assoc and really should be fixed at that level instead of post-processing the data... Commented Nov 26, 2015 at 10:39
  • Is not mysql , i knew that. Commented Nov 26, 2015 at 10:47
  • 1
    So where is this array coming from? How is it being generated? Commented Nov 26, 2015 at 11:12

3 Answers 3

2
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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it does exactly what i wanted! Thanks again
1

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);
?>

3 Comments

this will result in an empty array - you need a nested loop to iterate the second level arrays, all the 1st level indexes are numeric
You need the key from the outer loop as well, (or iterate by reference as &$arr) else you are just unsetting the element in a copy
@Steve yes i miss the reference and corrected. thank you. :-)
0

You can try this code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.