关于php:通过搜索Word排序多维数组

Sort Multidimensional Array By Searched Word

我需要通过搜索关键字对多维数组进行排序。我的数组如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
array(
  array(
    'name'    => '11th-Physics',
    'branch'  => 'Plus One',
    'college' => 'Plus One',
  ),
  array(
    'name'    => 'JEE-IIT',
    'branch'  => 'Physics',
    'college' => 'IIT College',
  ),
  array(
    'name'    => 'Physics',
    'branch'  => 'Bsc Physics',
    'college' => 'College of Chemistry',
  ),
  array(
    'name'    => 'Chemical Engineering',
    'branch'  => 'Civil',
    'college' => 'Physics Training Center',
  ),
  array(
    'name'    => 'Physics Education',
    'branch'  => 'Mechanical',
    'college' => 'TBR',
  ),
)
?>

当搜索关键字为physics时,我需要对这个数组进行排序。排序之后,我需要下面的结果。

NEEDED RESULT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
array(
  array(
    'name'    => 'Physics',
    'branch'  => 'Bsc Physics',
    'college' => 'College of Chemistry',
  ),
  array(
    'name'    => 'Physics Education',
    'branch'  => 'Mechanical',
    'college' => 'TBR',
  ),
  array(
    'name'    => '11th-Physics',
    'branch'  => 'Plus One',
    'college' => 'Plus One',
  ),
  array(
    'name'    => 'JEE-IIT',
    'branch'  => 'Physics',
    'college' => 'IIT College',
  ),
  array(
    'name'    => 'Chemical Engineering',
    'branch'  => 'Civil',
    'college' => 'Physics Training Center',
  ),
)

?>

也就是说,我需要首先按name对数组进行排序,这与搜索关键字完全相同。然后在name中进行通配符搜索。然后转到下一个键branch,与上面相同。有没有像我要求的那样对这个数组进行排序的PHP函数?我已经查过asortusort。但我没有得到正确的结果。


只需调用我为您的需求创建的这个简单函数,它就可以正常工作:)根据您的需要更改优先级顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function sortArray($array,$itemToSearch)
{
    $sortedArray = array();
    $priorityOrder = ['name','branch','college'];

    foreach ($priorityOrder as $key)
    {
        foreach ($array as $i => $value)
        {
            if(strpos(strtolower($value[$key]), strtolower($itemToSearch)) === 0)
            {
                array_push($sortedArray, $value);
                unset($array[$i]);
            }
        }
        foreach ($array as $i => $value)
        {
            if(strpos(strtolower($value[$key]), strtolower($itemToSearch)) > 0)
            {
                array_push($sortedArray, $value);
                unset($array[$i]);
            }
        }
    }

    return $sortedArray;
}


我们走吧

所以我从这个答案中的算法开始,修改它以满足您的需求。由于排序有三个不同的"优先级",所以我们必须使用一些临时变量来分隔希望排序的元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// arrays used to separate each row based on the row where the word"Physics" is found
$searchName = array();
$searchBranch = array();
$searchCollege = array();

// arrays used later for array_multisort
$foundInName = array();
$foundInBranch = array();
$foundInCollege = array();

foreach ($var as $key => $row) {
    if(strpos(strtolower($row['name']), 'physics') !== false) {
        $searchName[$key]  = $row['name'];
        $foundInName[] = $row;
    }
    elseif(strpos(strtolower($row['branch']), 'physics') !== false) {
        $searchBranch[$key]  = $row['branch'];
        $foundInBranch[] = $row;
    }
    elseif(strpos(strtolower($row['college']), 'physics') !== false) {
        $searchCollege[$key]  = $row['college'];
        $foundInCollege[] = $row;
    }
}

// Note: I use SORT_NATURAL here so that"11-XXXXX" comes after"2-XXXXX"
array_multisort($searchName, SORT_NATURAL, $foundInName);      // sort the three arrays separately
array_multisort($searchBranch, SORT_NATURAL, $foundInBranch);
array_multisort($searchCollege, SORT_NATURAL, $foundInCollege);

$sortedArray = array_merge($foundInName, $foundInBranch, $foundInCollege);

使用var_dump()输出$sortedArray给出如下公式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
array(5) {
    [0]=> array(3) {
        ["name"]=> string(12)"11th-Physics"
        ["branch"]=> string(8)"Plus One"
        ["college"]=> string(8)"Plus One"
    }
    [1]=> array(3) {
        ["name"]=> string(7)"Physics"
        ["branch"]=> string(11)"Bsc Physics"
        ["college"]=> string(20)"College of Chemistry"
    }
    [2]=> array(3) {
        ["name"]=> string(17)"Physics Education"
        ["branch"]=> string(10)"Mechanical"
        ["college"]=> string(3)"TBR"
    }
    [3]=> array(3) {
        ["name"]=> string(7)"JEE-IIT"
        ["branch"]=> string(7)"Physics"
        ["college"]=> string(11)"IIT College"
    }
    [4]=> array(3) {
        ["name"]=> string(20)"Chemical Engineering"
        ["branch"]=> string(5)"Civil"
        ["college"]=> string(23)"Physics Training Center"
  }
}

如你所见,11th-Physics首先出现。这是因为数字的ASCII值低于字母的值。要解决此问题,请通过在字符串前面加上一个高的ASCII字符来修改$search...数组。

1
2
3
4
5
if(strpos(strtolower($row['name']), 'physics') !== false) {
    // if the first character is a number, prepend an underscore
    $searchName[$key]  = is_numeric(substr($row['name'], 0, 1)) ? '_'.$row['name'] : $row['name'];
    $foundInName[] = $row;
}

产生以下输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
array(5) {
    [0]=> array(3) {
        ["name"]=> string(7)"Physics"
        ["branch"]=> string(11)"Bsc Physics"
        ["college"]=> string(20)"College of Chemistry"
    }
    [1]=> array(3) {
        ["name"]=> string(17)"Physics Education"
        ["branch"]=> string(10)"Mechanical"
        ["college"]=> string(3)"TBR"
    }
    [2]=> array(3) {
        ["name"]=> string(12)"11th-Physics"
        ["branch"]=> string(8)"Plus One"
        ["college"]=> string(8)"Plus One"
    }
    [3]=> array(3) {
        ["name"]=> string(7)"JEE-IIT"
        ["branch"]=> string(7)"Physics"
        ["college"]=> string(11)"IIT College"
    }
    [4]=> array(3) {
        ["name"]=> string(20)"Chemical Engineering"
        ["branch"]=> string(5)"Civil"
        ["college"]=> string(23)"Physics Training Center"
    }
}

试试看!