InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Finding Elements That Pass a Certain Test in PHP
PHP

Finding Elements That Pass a Certain Test in PHP

InfinityCoder November 20, 2016

You want to locate entries in an array that meet certain requirements.

Use a foreach loop:

1
2
3
4
5
$movies = array(/*...*/);
 
foreach ($movies as $movie) {
    if ($movie['box_office_gross'] < 5000000) { $flops[] = $movie; }
}

Or array_filter():

1
2
3
4
5
$movies = array(/* ... */);
 
$flops = array_filter($movies, function ($movie) {
    return ($movie['box_office_gross'] < 5000000) ? 1 : 0;
});

The foreach loops are simple: you iterate through the data and append elements to the return array that match your criteria.
If you want only the first such element, exit the loop using break:

1
2
3
4
$movies = array(/*...*/);
foreach ($movies as $movie) {
    if ($movie['box_office_gross'] > 200000000) { $blockbuster = $movie; break; }
}

You can also return directly from a function:

1
2
3
4
5
function blockbuster($movies) {
   foreach ($movies as $movie) {
       if ($movie['box_office_gross'] > 200000000) { return $movie; }
   }
}

With array_filter(), however, you first create an anonymous function that returns true for values you want to keep and false for values you don’t.

Using array_fil ter(), you then instruct PHP to process the array as you do in the foreach.
It’s impossible to bail out early from array_filter(), so foreach provides more flexibility and is simpler to understand.

Also, it’s one of the few cases in which the built-in PHP function doesn’t clearly outperform user-level code.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Validating Form Input: Checkboxes in PHP
You want to make sure only valid checkboxes are checked. …

Validating Form Input: Checkboxes in PHP

Uninstalling PEAR Packages in PHP
You wish to remove a PEAR package from your system. …

Uninstalling PEAR Packages in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap