Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Removing Blank Key from Array

Posted on 2011-04-25
5
Medium Priority
?
214 Views
Last Modified: 2012-05-11
I have an array that has an empty key set. I tried to remove it, but it's not working.

Here is the output:

Array ( [ ] => [200] => )

---------------

Here is the code I currently have:

            foreach($keys as $key => $value) {
                  if($key == "" || $key == " ") {
                unset($keys[$key]);
          }
    }

Can you let me know what I'm doing wrong?
0
Comment
Question by:deharvy
  • 2
  • 2
5 Comments
 
LVL 19

Assisted Solution

by:Greg Alexander
Greg Alexander earned 800 total points
ID: 35461797
I believe that keys maybe a protected variable in this case... try changing the variable name to test consider the following:
<?php
$test = array(''=>'ascas','1'=>'sdsd','You'=>'Cool');

print_r($test);
echo "<br>";
foreach($test as $key => $value) {

	if($key == "" || $key == " ") {
		unset($test[$key]);
	}
}


print_r($test);
?>

Open in new window

0
 
LVL 12

Assisted Solution

by:zappafan2k2
zappafan2k2 earned 1200 total points
ID: 35461954
How is the array being set in the first place?  Rather than cleaning up a mess, I usually prefer to avoid making one.

Otherwise, can you show us the output of var_dump($keys) instead of print_r?
0
 

Author Comment

by:deharvy
ID: 35462443
array(22) { [" "]=> NULL [200]=> NULL }
0
 
LVL 12

Accepted Solution

by:
zappafan2k2 earned 1200 total points
ID: 35462591
When I execute the following
<?php
$keys = array(' ' => NULL, '200' => NULL);
var_dump($keys);
foreach ($keys as $k => $v) {
    if ($k == ' ') unset ($keys[$k]); 
    }
var_dump($keys);
?>

Open in new window

I get
array(2) {
  [" "]=>
  NULL
  [200]=>
  NULL
}
array(1) {
  [200]=>
  NULL
}

Open in new window


If that doesn't work the same for you, there must be something funky about how the array is being assigned.  I'm also wondering why var_dump is showing array(22) instead of array(2).  Can you show us how the array is being generated?
0
 

Author Closing Comment

by:deharvy
ID: 35462848
Both these solutions work fine. Since my keys are numbers, was also able to use:

if($key == "" || $key < 1) {
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question