Solved

PHP: Remove items from an array if foobar empty

Posted on 2010-09-16
2
248 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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…
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…
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.

733 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