2017-08-27 11 views
1

検索キーワードで多次元配列をソートする必要があります。 私の配列は以下の通りです。検索ワードで多次元配列を並べ替える

<?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の場合、この配列を並べ替える必要があります。ソート後、以下のような結果が必要です。

私は正確に検索されたキーワードのようなものですnameで最初の配列をソートする必要があるNEEDED RESULT

<?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のワイルドカード検索。次に、次のキーbranchと上記と同じです。私の要件のようにこの配列をソートする関数はありますか?私はすでにasort、をチェックしています。しかし、私は結果を適切に得られませんでした。

+0

可能重複https://stackoverflow.com/questions/96759に応じて優先順位を変更する:)だけで正常に動作し、私はちょうどあなたの要件のために作成したこの簡単な関数を呼び出します/ how-do-i-sort-a-multidimensional-array-in-php –

+0

@WilliamPerronこれはキーで並べ替えられています。私は正規表現で値でソートする必要があります。 – AdhershMNair

答えて

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

あなたは素晴らしいBajiです。ありがとう。その完璧な作業。どうもありがとう。 – AdhershMNair

+0

Uは大歓迎です –

0

ここでは、だから私はthis answerにアルゴリズムから開始し、要件に合わせて、それを修正

を行きます。並べ替えには3つの異なる「優先順位」があるので、並べ替えたい要素を区切るために一時変数を使用する必要があります。あなたは11th-Physicsが最初に出てくる見ることができるように

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

var_dump()を使用して$sortedArrayを出力

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

のようなものを提供します。これは、数値がASCIIの値が文字の値よりも小さいためです。これを修正するには、文字列の前に高いASCII文字を付加して$search...配列を変更します。次の出力得

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それを試してみてください!

関連する問題