duanjiwu0324 2016-04-12 21:40
浏览 54
已采纳

(PHP)如何使用与变量相关的字符串来命令多维数组?

Hey I have a new feature on my website where you can search for ya items by entering some keywords in the search input. Of course I am using ajax and I need a new function which sort or order the array which holds the item info from the database related to the keyword which the user writes in the input field. When the user hits for example "m" so it should be displayed in this way.

array [["836","123","Malaga - Ocean","1"],
       ["834","123","Malaga City","1"],
       ["838","123","DayZ - Novo Turm #1","0"],
       ["839","123","DayZ - Novo Turm #2","0"],
       ["840","123","DayZ - Novo Turm #3","0"]]

But if Im searching for something which begins with the letter "m", "Malaga City" and "Malaga - Ocean" should be displayed first in the result.

I am searching more then one table for the keyword where I collect it. But if I am adding the array in every different table results I get so every time the picture table results first.

MySql:

$sql_picture = "SELECT * FROM pictures [...] ORDER BY name LIMIT 5";
$sql_videos = "SELECT * FROM videos [...] ORDER BY name LIMIT 5";
$sql_audio = "SELECT * FROM audio [...] ORDER BY name LIMIT 5";
$sql_documents = "SELECT * FROM documents [...] ORDER BY name LIMIT 5";

PHP:

$collectedDataName = array();

for($i = 0; $i < count($collectData); $i++){
    $collectedDataName[$i] = $collectData[$i][2];
}

$collectData_lowercase = array_map('strtolower', $collectedDataName);

array_multisort($collectData_lowercase, SORT_ASC, SORT_STRING, $collectedDataName);

$returnData = array();

for($j = 0; $j < 5; $j++){
    for($i = 0; $i < count($collectData); $i++){
        if($collectData[$i][2] === $collectedDataName[$j]){
            $returnData[$j] = $collectData[$i];
        }
    }
}

echo json_encode($returnData);

Result:

array [["838","123","DayZ - Novo Turm #1","0"],
      ["839","123","DayZ - Novo Turm #2","0"],
      ["840","123","DayZ - Novo Turm #3","0"],
      ["836","123","Malaga - Ocean","1"],
      ["834","123","Malaga City","1"]]

How can I sort my multi dimensonal array by its string value related to a variable?

  • 写回答

3条回答 默认 最新

  • dongpeng8994 2016-04-13 16:56
    关注

    You want to sort your multi dimensional array based on the input value, a little bit like we do in sql using

    select * from table where column like 'inputValue%'
    

    This is a solution based on php only

    $inputvalue = 'm';
    $inputvalue = strtolower($inputvalue);
    
    $collectData = [["838","123","DayZ - Novo Turm #1","0"],
                 ["834","123","Malaga City","1"],
                 ["839","123","DayZ - Novo Turm #2","0"],         
                 ["836","123","Malaga - Ocean","1"],
                 ["840","123","DayZ - Novo Turm #3","0"],
                ];
    
    /*
    we use strpos to check if the input value is contained in the strings array, if not we use levenshtein to calculate the distance between them.*/
    
    $distances = array();
    $positions = array();
    for ($i=0; $i < count($collectData); $i++) {    
      $current_val = strtolower($collectData[$i][2]);
      if (strpos($current_val, $inputvalue)!==false)
        $distance = strpos($current_val, $inputvalue);
      else
        $distance = levenshtein($inputvalue, $current_val);
    
      $distances[] = $distance;        
      $positions[] = array("distance"=>$distance, "position"=>$i);        
    }
    
    /*we sort distances*/
    sort($distances);
    
    /* we reorder collectData based on distances sort  */
    $returnData = array();
    for ($j=0; $j<count($distances); $j++) {
      for ($k=0; $k<count($positions); $k++) {
        $val = $collectData[$positions[$k]["position"]];
    
        if ($distances[$j]==$positions[$k]["distance"] && !in_array($val, $returnData)) {
            $returnData[] = $val;
            break;
        }
      }
    }
    
    return $returnData;
    

    levenshtein php doc here : levenshtein function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大