通过搜索词排序多维数组

时间:2017-08-27 15:40:50

标签: php arrays sorting multidimensional-array

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

<?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时,我需要对此数组进行排序。排序后,我需要如下结果。

  

需要结果

<?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。但我没有得到正确的结果。

2 个答案:

答案 0 :(得分:1)

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

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 :(得分:0)

我们走了

所以我从this answer中的算法开始,并根据您的要求进行修改。因为你有三个不同的优先级&#34;为了排序,我们必须使用一些临时变量来分隔我们希望排序的元素。

// 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);

使用$sortedArray输出var_dump()的内容如下:

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值低于字母的值。要解决此问题,请通过在字符串前添加高$search...字符来修改ASCII数组。

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;
}

产生以下输出:

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"
    }
}

试一试here