<?php
$json = '[
{"title":"The Call of the Wild","author":"Jack London"},
{"title":"Great Expectations","author":"Charles Dickens"},
{"title":"The Voyage of the Beatle","author":"Charles Darwin"}
]';
$array = json_decode($json, true);
function searchArrayKeyVal($search, $array) {
$results = array();
// search for string in each column
foreach ($array as $idx => $obj) {
foreach ($obj as $key => $value) {
if (stripos($value, $search) !== false) {
array_push($results, $obj);
break;
}
}
}
return $results;
}
print_r(searchArrayKeyVal('charles', $array));
print_r(searchArrayKeyVal('wild', $array));
preferences:
72.49 ms | 402 KiB | 5 Q