0

My question may seem fairly simple, but I have tried multiple techniques, and none seem to yield a proper answer.

I have an associative array as follows:

$array = array("TB1_course" => array(null, 'CHEM 2E03', null, "BIO 1A03"),  
          "TB1_section" => array(null, 'CHEM 2E03', null, "BIO 1A03"), 
          "TB1_session" => array(null, 'CHEM 2E03', null, "BIO 1A03")
          );

Now I would like to remove all the null elements in my arrays, for the respective associative array.

My attempt was as follows:

foreach($array as $key=>$value){
    for($i=0; $i<sizeof($value);$i++){
        if ($value[$i]==null){
           unset($value[$i]); 
        }
        $array[$key]=$value;
    }
}

print_r($array);

But my output is also retianing the indexes of the array. My output is as follows:

Array
(
[TB1_course] => Array
    (
        [1] => CHEM 2E03
        [3] => BIO 1A03
    )

[TB1_section] => Array
    (
        [1] => CHEM 2E03
        [3] => BIO 1A03
    )

[TB1_session] => Array
    (
        [1] => CHEM 2E03
        [3] => BIO 1A03
    )

)

I would like to remove the indexes, so that there are only two elements inside of my arrays. "CHEM 2E03" should be the 0th index, and "BIO 1A03" should be the 1st index. I am using PHP 5.4.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad
  • 604
  • 5
  • 14
  • 29

5 Answers5

1

The array_values() function retains the value and resets the indexed of the array. Below is it's implementation for your purpose:

foreach($array as $key=>$value){
  for($i=0; $i<sizeof($value);$i++){
    if ($value[$i]==null){
       unset($value[$i]); 
    }
    $array[$key] = array_values($value);
  }
}
coderodour
  • 1,072
  • 8
  • 16
1

You can use the array_values() function to re-index your array.

Brian Glaz
  • 15,468
  • 4
  • 37
  • 55
0

Using unset() removes the value but keeps the key as it is. The best solution here will be to use array_splice() which will remove the element completely. For your code, it should be

array_splice($value, $i, 1);

instead of

unset($value[$i]); 
Mo-
  • 155
  • 15
0

You can use a function then this will recursively filter and reset your indexe using array_filter and array_values

function array_filter_recursive($input) 
{ 
    foreach ($input as &$value) 
    { 
        if (is_array($value)) 
        { 
            $value = array_filter_recursive($value); 
        } 
    } 
    return array_values(array_filter($input)); 
} 

$array = [
    "TB1_course" => array(null, 'CHEM 2E03', null, "BIO 1A03"),  
    "TB1_section" => array(null, 'CHEM 2E03', null, "BIO 1A03"), 
    "TB1_session" => array(null, 'CHEM 2E03', null, "BIO 1A03")
];

print_r(array_filter_recursive($array));

Here is a phpfiddle to run the above: http://phpfiddle.org/main/code/cgbj-h10z

Resources
1. http://php.net/manual/en/function.array-filter.php
2. http://php.net/manual/en/function.array-values.php

Dan Mason
  • 2,177
  • 1
  • 16
  • 38
0

try

foreach($array as $key=>$value){
    for($i=0; $i<sizeof($value);$i++){
        if ($value[$i]==null){
           unset($value[$i]); 
        }
        $array[$key]=$value;
    }
}

$arr = array_map('array_values', $array);
print_r($arr);
Dario
  • 417
  • 4
  • 17