0

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.

1
  • loop through them and use unset() as simple as that Commented Jan 15, 2015 at 15:41

2 Answers 2

1

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

4 Comments

Thank you very much for taking the time to answer. Unfortunately, the result I got here, after applying your recommendation, indicates that the empty sub-arrays are still being displayed. Maybe I applied your code incorrectly? http://hastebin.com/geyusosiyo
Thanks again. I am so sorry for taking your time. The following code, after your new recommendation, still returns those empty arrays: http://hastebin.com/umijaqufel.
@thiago Ahhh, now i see. The arrays are not empty, they contain a single space character. See my latest revision for a (tested, working) solution
I knew it was something I did wrong. Thank you for your solution and for being so patient.
1

As an example of using array_filter() and assuming it is always an empty single element array:

$new_array = array_filter($array, function($elements) {
    return !empty($elements[0]);
});

1 Comment

Mr. McCreary, I sincerely thank you for your answer. I followed your recommendation but, unfortunately, the empty arrays are still being displayed. Link to the code I used to test: http://hastebin.com/tucipapaco. Maybe I'm implementing your recommendation incorrectly?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.