
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Search for partial value match in an Array in PHP
The array_filter function can be used to match a partial value in an array. A callback can be provided, that helps in deciding which elements would remain in the array and which would be removed.
When the callback returns false, it means the given element needs to be removed. Below is a code example demonstrating the same −
$arr = array(0 => 'abc', 1 => 'def', 2 => 'ghijk', 3 => 'lmnxyz'); $results = array(); foreach ($arr as $value) { if (strpos($value, 'xyz') !== false) { $results[] = $value; } } if( empty($results) ) { echo 'No matches found.'; } else { echo "'xyz' was found in: " . implode('; ', $results); }
Parsing JSON array with PHP foreach −
'xyz' was found in: lmnxyz
- Related Articles
- MongoDB query for Partial Object in an array
- Search for PHP array element containing string?
- How to search for an element in JavaScript array?
- How to search for an array element in TypeScript?
- How to locate element by partial id match in Selenium?
- Search in an array with Binary search using JavaScript
- How to create an array of partial objects from another array in JavaScript?
- How to get random value out of an array in PHP?
- Search for documents matching first item in an array with MongoDB?
- Sort php multidimensional array by sub-value in PHP
- Implementing partial sum over an array using JavaScript
- C# Program to search for a string in an array of strings
- JavaScript Program for Search an element in a sorted and rotated array
- Java Program for Search an element in a sorted and rotated array
- Implement MongoDB $addToSet for an array in an array and append a value

Advertisements