Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP: Remove items from an array if foobar empty

Posted on 2010-09-16
2
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 count occurrences of each item in an array.
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…
Suggested Courses

618 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