2

Have this multidimensional array. How can I remove this element?

 [1]=>
    array(0) {
    }

Tried unset(), array_pop() but both do not work.

Example with unset():

echo "before\n";
var_dump($body['body'][0][1]);
unset($body['body'][0][1][1]);
echo "after\n";
var_dump($body['body'][0][1]);

before
array(4) {
  ["token_name"]=>
  string(7) "C_BLOCK"
  ["token_group"]=>
  string(7) "C_BLOCK"
  ["group"]=>
  bool(true)
  ["body"]=>
  array(2) {
    [0]=>
    array(1) {
      [0]=>
      array(5) {
        ["token_name"]=>
        string(12) "C_CASE_BLOCK"
        ["token_group"]=>
        string(12) "C_CASE_BLOCK"
        ["group"]=>
        bool(true)
        ["args"]=>
        array(1) {
          [0]=>
          array(6) {
            ["token_name_org"]=>
            string(9) "T_LNUMBER"
            ["token"]=>
            int(317)
            ["value"]=>
            string(2) "10"
            ["line"]=>
            int(4)
            ["token_group"]=>
            string(9) "VARIABLES"
            ["token_name"]=>
            string(8) "C_NUMBER"
          }
        }
        ["body"]=>
        array(1) {
          [0]=>
          array(2) {
            [0]=>
            array(6) {
              ["token_name_org"]=>
              string(6) "T_ECHO"
              ["token"]=>
              int(328)
              ["value"]=>
              string(4) "echo"
              ["line"]=>
              int(5)
              ["token_group"]=>
              string(9) "FUNCTIONS"
              ["token_name"]=>
              string(6) "C_ECHO"
            }
            [1]=>
            array(6) {
              ["token_name_org"]=>
              string(26) "T_CONSTANT_ENCAPSED_STRING"
              ["token"]=>
              int(323)
              ["value"]=>
              string(4) "test"
              ["line"]=>
              int(5)
              ["token_group"]=>
              string(7) "STRINGS"
              ["token_name"]=>
              string(8) "C_STRING"
            }
          }
        }
      }
    }
    [1]=>
    array(0) {
    }
  }
}
after
array(4) {
  ["token_name"]=>
  string(7) "C_BLOCK"
  ["token_group"]=>
  string(7) "C_BLOCK"
  ["group"]=>
  bool(true)
  ["body"]=>
  array(2) {
    [0]=>
    array(1) {
      [0]=>
      array(5) {
        ["token_name"]=>
        string(12) "C_CASE_BLOCK"
        ["token_group"]=>
        string(12) "C_CASE_BLOCK"
        ["group"]=>
        bool(true)
        ["args"]=>
        array(1) {
          [0]=>
          array(6) {
            ["token_name_org"]=>
            string(9) "T_LNUMBER"
            ["token"]=>
            int(317)
            ["value"]=>
            string(2) "10"
            ["line"]=>
            int(4)
            ["token_group"]=>
            string(9) "VARIABLES"
            ["token_name"]=>
            string(8) "C_NUMBER"
          }
        }
        ["body"]=>
        array(1) {
          [0]=>
          array(2) {
            [0]=>
            array(6) {
              ["token_name_org"]=>
              string(6) "T_ECHO"
              ["token"]=>
              int(328)
              ["value"]=>
              string(4) "echo"
              ["line"]=>
              int(5)
              ["token_group"]=>
              string(9) "FUNCTIONS"
              ["token_name"]=>
              string(6) "C_ECHO"
            }
            [1]=>
            array(6) {
              ["token_name_org"]=>
              string(26) "T_CONSTANT_ENCAPSED_STRING"
              ["token"]=>
              int(323)
              ["value"]=>
              string(4) "test"
              ["line"]=>
              int(5)
              ["token_group"]=>
              string(7) "STRINGS"
              ["token_name"]=>
              string(8) "C_STRING"
            }
          }
        }
      }
    }
    [1]=>
    array(0) {
    }
  }
}

Why it is shown anyway, doesn't array(0) means an array with zero elements? Why it is there than?

   [1]=>
        array(0) {
        }
3
  • 1
    and with unset($body['body'][0][1]['body'][1]); ? Commented Jun 9, 2017 at 11:43
  • as i can see you were unseting that key which is not exist try @AlessandroMinoccheri answer Commented Jun 9, 2017 at 11:48
  • THANK YOU!!! @CasimiretHippolyte WORKS!!!! Was looking at indexing, but could not figure it out. Your solution works! Commented Jun 9, 2017 at 11:49

2 Answers 2

3

try this please:

unset($body['body'][1]);

UPDATE

try this:

unset($body['body'][0][1]['body'][1]);

You need to unset the right index

Sign up to request clarification or add additional context in comments.

2 Comments

Take care that the dump doesn't display $body but $body['body'][0][1].
Glad to help you!
0

You can try something like this out:

$clear = array();
foreach($your_array_variable as $key=>$val){
    $val = '';
    $clear [$key] = $val;
}

print_r($clear); reference: more

OR

foreach (my_array_keys($array, 'apple') as $key) {
    unset($array[$key]);
}

OR

// destroy a single variable
unset($foo);

// destroy a single element of an array
unset($bar['body']);

// destroy more than one variable
unset($foo1, $foo2, $foo3);

Comments

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.