Tuesday 4 September 2018

PHP Remove empty values ​​from a multidimensional array

I researched a lot of questions and answers posted on stackoverflow about the subject but I still couldn't find a solution to the following problem.

I need to generate an array from the text provided by this link, minus some specific lines: https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf. This part I was able to do.
PHP Code:
$arr = file('https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf');
$arr = preg_replace('/^(?![a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}\s).*$/i', '', $arr);
$arr = preg_replace ('/#/i', '',$arr);
$arr = preg_replace('/\t+/', ' ', $arr);
$arr = preg_replace('/\s{2,}/', ' ', $arr);
$arr = preg_replace('/ /', '~', $arr, 2);

foreach ($arr as $field)
{
    $macList[] = explode('~', $field);
}

echo "<pre>";
print_r($macList);
echo "</pre>";

The problem is that when I use print_r, the first 52 sub-arrays are empty, plus some dozens at the bottom. I can't figure out how to remove them.
Reduced print_r output, because the full output returns 25770 sub-arrays:
Array
(

[0] => Array
    (
        [0] => 

    )

[1] => Array
    (
        [0] => 

    )

[2] => Array
    (
        [0] => 

    )

[3] => Array
    (
        [0] => 

    )

[4] => Array
    (
        [0] => 

    )

[5] => Array
    (
        [0] => 

    )

[6] => Array
    (
        [0] => 

    )

[7] => Array
    (
        [0] => 

    )

[8] => Array
    (
        [0] => 

    )

[9] => Array
    (
        [0] => 

    )

[10] => Array
    (
        [0] => 

    )

[11] => Array
    (
        [0] => 

    )

[12] => Array
    (
        [0] => 

    )

[13] => Array
    (
        [0] => 

    )

[14] => Array
    (
        [0] => 

    )

[15] => Array
    (
        [0] => 

    )

[16] => Array
    (
        [0] => 

    )

[17] => Array
    (
        [0] => 

    )

[18] => Array
    (
        [0] => 

    )

[19] => Array
    (
        [0] => 

    )

[20] => Array
    (
        [0] => 

    )

[21] => Array
    (
        [0] => 

    )

[22] => Array
    (
        [0] => 

    )

[23] => Array
    (
        [0] => 

    )

[24] => Array
    (
        [0] => 

    )

[25] => Array
    (
        [0] => 

    )

[26] => Array
    (
        [0] => 

    )

[27] => Array
    (
        [0] => 

    )

[28] => Array
    (
        [0] => 

    )

[29] => Array
    (
        [0] => 

    )

[30] => Array
    (
        [0] => 

    )

[31] => Array
    (
        [0] => 

    )

[32] => Array
    (
        [0] => 

    )

[33] => Array
    (
        [0] => 

    )

[34] => Array
    (
        [0] => 

    )

[35] => Array
    (
        [0] => 

    )

[36] => Array
    (
        [0] => 

    )

[37] => Array
    (
        [0] => 

    )

[38] => Array
    (
        [0] => 

    )

[39] => Array
    (
        [0] => 

    )

[40] => Array
    (
        [0] => 

    )

[41] => Array
    (
        [0] => 

    )

[42] => Array
    (
        [0] => 

    )

[43] => Array
    (
        [0] => 

    )

[44] => Array
    (
        [0] => 

    )

[45] => Array
    (
        [0] => 

    )

[46] => Array
    (
        [0] => 

    )

[47] => Array
    (
        [0] => 

    )

[48] => Array
    (
        [0] => 

    )

[49] => Array
    (
        [0] => 

    )

[50] => Array
    (
        [0] => 

    )

[51] => Array
    (
        [0] => 

    )

[52] => Array
    (
        [0] => 00:00:00
        [1] => 00:00:00
        [2] => Officially Xerox, but 0:0:0:0:0:0 is more common

    )
)

Question: How do I remove these 52 (0 to 51) empty arrays? Nothing I tried worked.

Rather than trying to remove the empty keys, instead dont add the empty arrays in the 1st place: EDIT is seems the 'empty' arrays are not in fact empty, but instead contain a single element with a space character. To remove you can combine empty with trim , array_map and array_filter:
  foreach ($arr as $field)
  {
      $temp = explode('~', $field);
      if(!empty(array_filter(array_map('trim', $temp))))
      {
          $macList[] = $temp;
      }
  }

  echo "<pre>";
  print_r($macList);
  echo "</pre>";

0 comments:

Post a Comment