In my $data array i have some data stored. So when i try to search for something which is in the right direction my function can find within the array.
For example ---
Suppose i am looking for **Samsung Galaxy S** from my array $data,
it will return the value 1, 2 and 4 from the array
but the problem is i want to find in data in a random way. like - "S Galaxy Samsung"
But the data is stored like - Samsung GT-i9100 Galaxy S II so when i search for "Samsung Galaxy S", it actually find the value. But when i search for 'S Galaxy Samsung', it can not find the value because we are finding in a random way.
But it should be found in the array, because the request data is there .
Anyone knows any solution for this problem !!!!
function fetchDataAction() {
$_POST = 'S Galaxy Samsung';
$search = $this->my_array_search($data, $_POST); // $data is the array
}
function my_array_search($array, $string) {
$pattern = preg_replace('/\s+/', ' .*', preg_quote($string));
return array_filter($array, function ($value) use($pattern) {
return preg_match('/' . $pattern . '/', $value) == 1;
});
}
$data =
Array
(
[0] => Array
(
[name] => Samsung GT-N7100 Galaxy Note II 16GB
)
[1] => Array
(
[name] => Samsung GT-i9100 Galaxy S II
)
[2] => Array
(
[name] => Samsung GT-i9300 Galaxy S III 16GB
)
[3] => Array
(
[name] => Apple iPhone 5 16GB
)
[4] => Array
(
[name] => Samsung GT-P5110 Galaxy S 4 10.1 16GB
)
[5] => Array
(
[name] => Samsung UE46ES6715
)
[6] => Array
(
[name] => Samsung 830 Series MZ-7PC128 128GB
)
[7] => Array
(
[name] => Samsung GT-N8000 Galaxy Note 10.1 16GB
)
[8] => Array
(
[name] => Samsung 830 Series MZ-7PC256 256GB
)
[9] => Array
(
[name] => Samsung UE46ES6715
)
[10] => Array
(
[name] => Samsung GT-2423 Galaxy Tab 4 10.1 16GB
)