0

Suppose I have an array which is in the below:

Array
(
    [0] => Array
        (
            [name] => playstation sony 3 slim 320gb
        )

    [1] => Array
        (
            [name] => sony xperia acro s
        )

    [2] => Array
        (
            [name] => sony xperia tipo
        )

    [3] => Array
        (
            [name] => sony ericsson xperia arc s
        )

    [4] => Array
        (
            [name] => sony xperia go
        )

    [5] => Array
        (
            [name] => sony playstation 4
        )

)

Now I want to filter the array based on the search value and sort the array by the based on most relevant (by shortest length).

My request data is:

 $request = 'sony';

I'm expecting the following results:

 Array
(
    [0] => Array
        (
            [name] => sony xperia go
        )

    [1] => Array
        (
            [name] => sony xperia tipo
        )

    [2] => Array
        (
            [name] => sony playstation 4
        )

    [3] => Array
        (
            [name] => sony xperia sony acro ss
        )

    [4] => Array
        (
            [name] => sony ericsson xperia arc s
        )

    [5] => Array
        (
            [name] => playstation Sony 3 slim 320gb
        )

)
17
  • 3
    How it is possible in PHP - Yes, it is possible. What have you tried? Commented Oct 12, 2015 at 16:24
  • 2
    BTW, you have declared an array with the same keys. Commented Oct 12, 2015 at 16:26
  • @Federico i do not know how to call that function does this !!! i have search for t, but did not get any result how to do that, can i get at least some advice which way i should do that Commented Oct 12, 2015 at 16:29
  • 1
    Iterate over the array, and search every value with substr_count. Commented Oct 12, 2015 at 16:31
  • 1
    Try filter array by array_filter then sort it by usort. Commented Oct 12, 2015 at 18:19

1 Answer 1

3

The simplest way to achieve your search filtering in PHP is to filter array (array_filter) by your custom callback then sort it using a user-defined comparison function (usort).

Here is a simple code:

<?php 
$data[]['name'] = 'foo';
$data[]['name'] = 'playstation sony 3 slim 320gb';
$data[]['name'] = 'sony xperia acro s';
$data[]['name'] = 'sony xperia tipo';
$data[]['name'] = 'sony ericsson xperia arc s';
$data[]['name'] = 'sony xperia go';
$data[]['name'] = 'sony playstation 4';
$data[]['name'] = 'bar';

$result = array_filter($data, function($v) { return stristr(current($v), 'sony'); });
usort($result, function($a, $b) { return strlen(current($a)) < strlen(current($b)) ? -1 : 1; });
print_r($result);

Please note that PHP is not a good solution for implementation of complex search requirements. So for more search-oriented solutions, please consider using:

which can be easily integrated with PHP.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.