Solved

PHP: Remove items from an array if foobar empty

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now