code_junkie

How do you remove a value that has an empty key from an associative array in PHP?

Nov 11th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. foreach ($array as $index => $value) {
  2. echo $index;
  3. echo ' is ';
  4. echo gettype($index);
  5. echo "n";
  6. }
  7.  
  8. $someList = Array('A' => 'Foo', 'B' => 'Bar', '' => 'Bah');
  9. print_r($someList);
  10. echo '<br/>';
  11. unset($someList['A']);
  12. print_r($someList);
  13. echo '<br/>';
  14. unset($someList['']);
  15. print_r($someList);
  16. echo '<br/>';
  17.  
  18. Array ( [A] => Foo [B] => Bar [] => Bah )
  19. Array ( [B] => Bar [] => Bah )
  20. Array ( [B] => Bar )
  21.  
  22. <?php
  23.  
  24. $arr = array(
  25. false => 1
  26. , true => 2
  27. , null => 3
  28. , 'test' => 4
  29. // , '' => 5
  30. );
  31.  
  32. print_r( $arr );
  33.  
  34. foreach ( $arr as $key => $value )
  35. {
  36. var_dump( $key );
  37. }
  38.  
  39. unset( $arr[''] );
  40.  
  41. print_r( $arr );
  42.  
  43. Array
  44. (
  45. [0] => 1
  46. [1] => 2
  47. [] => 3
  48. [test] => 4
  49. )
  50. int(0)
  51. int(1)
  52. string(0) ""
  53. string(4) "test"
  54. Array
  55. (
  56. [0] => 1
  57. [1] => 2
  58. [test] => 4
  59. )
  60.  
  61. var_export($array);
  62. echo "n";
  63. unset($array[""]);
  64. var_export($array);
  65.  
  66. array (
  67. '' => 1,
  68. )
  69. array (
  70. )
Add Comment
Please, Sign In to add comment