Solved

PHP: Remove items from an array if foobar empty

Posted on 2010-09-16
2
247 Views
Last Modified: 2012-05-10
If foobar is empty then I want to remove the entire item from my array.
<?php

$a = array();
$a[]=array('id'=>1,'Name'=>'Abc','foobar'=>null);
$a[]=array('id'=>2,'Name'=>'Jkl','foobar'=>'Hello');
$a[]=array('id'=>3,'Name'=>'Qrs','foobar'=>'Test');
$a[]=array('id'=>4,'Name'=>'Xyz','foobar'=>'');

echo '<pre>';
print_r($a);
echo '</pre>';

?>

Open in new window

0
Comment
Question by:hankknight
  • 2
2 Comments
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 33693603
<?php

$a = array();
$a[]=array('id'=>1,'Name'=>'Abc','foobar'=>null);
$a[]=array('id'=>2,'Name'=>'Jkl','foobar'=>'Hello');
$a[]=array('id'=>3,'Name'=>'Qrs','foobar'=>'Test');
$a[]=array('id'=>4,'Name'=>'Xyz','foobar'=>'');

for($i=0; $i<array_len($a); $i++)
{
  if($a[$i]['foobar'] == '')
  {
    $a[$i] = NULL;
  }
}
echo '<pre>';
print_r($a);
echo '</pre>';

?>
0
 
LVL 10

Accepted Solution

by:
Tyler Laczko earned 500 total points
ID: 33693935
sorry. this is MUCH BETTER!!!!


<?php

$a = array();
$a[]=array('id'=>1,'Name'=>'Abc','foobar'=>null);
$a[]=array('id'=>2,'Name'=>'Jkl','foobar'=>'Hello');
$a[]=array('id'=>3,'Name'=>'Qrs','foobar'=>'Test');
$a[]=array('id'=>4,'Name'=>'Xyz','foobar'=>'');

foreach($a as $a1)
      if($a1['foobar'] != '' && $a1['foobar'] != NULL)
            $new_array[] = $a1;
      
echo '<pre>';
print_r($new_array);
echo '</pre>';


?>
<?php

$a = array();
$a[]=array('id'=>1,'Name'=>'Abc','foobar'=>null);
$a[]=array('id'=>2,'Name'=>'Jkl','foobar'=>'Hello');
$a[]=array('id'=>3,'Name'=>'Qrs','foobar'=>'Test');
$a[]=array('id'=>4,'Name'=>'Xyz','foobar'=>'');

foreach($a as $a1)
	if($a1['foobar'] != '' && $a1['foobar'] != NULL)
		$new_array[] = $a1;
	
echo '<pre>';
print_r($new_array);
echo '</pre>';


?>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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