I'm trying to get the data in the array according to the input value, like the example below, if I enter the correct value, it will get the value, but when I enter the incorrect value, it doesn't work.
You can see a small example below.
<?php
echo "<pre>";
$array = [
[
"id" => "33704",
"name" => "Total apple"
],
[
"id" => "33706",
"name" => "Used apple"
],
[
"id" => "33694",
"name" => "banana: Bits received"
],
[
"id" => "33697",
"name" => "banana: Bits sent"
]
];
print_r($array);
function searchItem($array, $key, $value)
{
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
$results = array_merge($results, searchItem($subarray, $key, $value));
}
}
return $results;
}
$result = searchItem($array, 'name', 'Bits received');
print_r($result);
When I enter the exact value 'banana: Bits received' it can be retrieved, but the value is many and unlimited, I want when I enter 'Bits received' it must also be found. Hope everyone can help. Thanks!