13

I have a key that appears to be an empty string, however using unset($array[""]); does not remove the key/value pair. I don't see another function that does what I want, so I'm guessing it's more complicated that just calling a function.

The line for the element on a print_r is [] => 1, which indicates to me that the key is the empty string.

Using var_export, the element is listed as '' => 1.

Using var_dump, the element is listed as [""]=>int(1).

So far, I have tried all of the suggested methods of removal, but none have removed the element. I have tried unset($array[""]);, unset($array['']);, and unset($array[null]); with no luck.

Thomas Owens
  • 114,398
  • 98
  • 311
  • 431

6 Answers6

24

Try unset($array[null]);

If that doesn't work, print the array via var_export or var_dump instead of print_r, since this allows you to see the type of the key. Use var_export to see the data in PHP syntax.

var_export($array);

Note that var_export does not work with recursive structures.

Lemming
  • 834
  • 1
  • 7
  • 16
  • Somewhere along the line, a null value was getting transformed into the empty string. By going to the very source of the array and unsetting null values, I was able to correct the problem. – Thomas Owens Oct 28 '08 at 14:43
  • 2
    From http://php.net/manual/en/language.types.array.php: "Using TRUE as key will evaluate to integer 1 as a key. Using FALSE as key will evaluate to integer 0 as a key. Using NULL as a key will evaluate to the empty string. Using the empty string as a key will create (or overwrite) a key with the empty string and its value;" – okalex Jul 13 '10 at 17:46
  • In my case, I had to use double quotes. `unset($array[""]);` – Tomas Gonzalez Jul 21 '14 at 19:48
  • 1
    @TomasGonzalez in PHP, `""` and `''` are identical when they are empty. – SOFe Jul 21 '17 at 16:27
  • @SOFe True! I don't even remember why I wrote that. But I guess I simply meant: "an empty array". – Tomas Gonzalez Jul 21 '17 at 19:56
2

Tried:

$someList = Array('A' => 'Foo', 'B' => 'Bar', '' => 'Bah');
print_r($someList);
echo '<br/>';
unset($someList['A']);
print_r($someList);
echo '<br/>';
unset($someList['']);
print_r($someList);
echo '<br/>';

Got:

Array ( [A] => Foo [B] => Bar [] => Bah )
Array ( [B] => Bar [] => Bah )
Array ( [B] => Bar )

You should analyse where the key come from, too...

PhiLho
  • 40,535
  • 6
  • 96
  • 134
1

My guess is that it's not an empty string. Try the following to see what you get:

foreach ($array as $index => $value) {
    echo $index;
    echo ' is ';
    echo gettype($index);
    echo "\n";
}
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
1

Try using var_dump instead of print_r. This may give you a better idea of what exactly the key is.

Lucas Oman
  • 15,597
  • 2
  • 44
  • 45
1

Not sure what to tell you. Running this script

<?php

$arr = array(
        false   => 1
    ,   true    => 2
    ,   null    => 3
    ,   'test'  => 4
//  ,   ''      => 5
);

print_r( $arr );

foreach ( $arr as $key => $value )
{
    var_dump( $key );
}

unset( $arr[''] );

print_r( $arr );

I get the following output

Array
(
    [0] => 1
    [1] => 2
    [] => 3
    [test] => 4
)
int(0)
int(1)
string(0) ""
string(4) "test"
Array
(
    [0] => 1
    [1] => 2
    [test] => 4
)

See how the "null" array key was type converted to an empty string?

Are you sure you are not working with a copy of the array? If you did this call to unset() from inside a function, it's possible that you are.

This was tested on PHP 5.2.0

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
  • Wow, I hadn't realized that null gets converted to an empty string when used as an array key. Crazy behaviour. unset($array[null]) just works like unset($array['']) then. – Lemming Oct 28 '08 at 14:55
0

Please post the code you use to remove the element as well your checker code before and after that line.

What I'm looking for is something like this:

var_export($array);
echo "\n";
unset($array[""]);
var_export($array);

Please also post the complete output of both var_export lines.

I'm looking for something like this:

array (
  '' => 1,
)
array (
)
Lemming
  • 834
  • 1
  • 7
  • 16
  • I was (and am) not sure, that you're checking things correctly. No offense, but the code should work. The fact that it doesn't indicated that one of our assumptions is incorrect. To verify, I wanted to see the uncensored, unabbreviated, full code, not a summarized version. – Lemming Oct 28 '08 at 14:34