How can you extract subarrays from an array based on a specific keyword in a value in PHP?
To extract subarrays from an array based on a specific keyword in a value in PHP, you can loop through the array and check if the keyword exists in the value of each element. If the keyword is found, you can add that element to a new array of subarrays. This can be achieved by using a foreach loop and an if statement to filter out the elements that match the keyword.
<?php
// Sample array with values
$array = array(
array('name' => 'John Doe', 'age' => 30),
array('name' => 'Jane Smith', 'age' => 25),
array('name' => 'Alice Johnson', 'age' => 35),
);
$keyword = 'Jane'; // Keyword to search for in the values
$subarrays = array();
foreach ($array as $element) {
if (strpos($element['name'], $keyword) !== false) {
$subarrays[] = $element;
}
}
print_r($subarrays);
?>
Related questions
-
database design normalization scalability
-
PHP concatenation syntax
-
PHP SQL query email confirmation