gpt4 book ai didi

php - (PHP) 如何对包含与变量相关的字符串的多维数组进行排序?

转载 作者:行者123 更新时间:2023-11-29 02:17:55 27 4
gpt4 key购买 nike

嘿,我的网站上有一个新功能,您可以通过在搜索输入中输入一些关键字来搜索您的项目。当然,我正在使用 ajax,我需要一个新函数来对数组进行排序或排序,该数组包含数据库中与用户在输入字段中写入的关键字相关的项目信息。例如,当用户点击“m”时,它应该以这种方式显示。

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

但是如果我搜索以字母“m”开头的内容,“Malaga City”和“Malaga - Ocean”应该首先显示在结果中。

我在多个表格中搜索我收集关键字的表格。但是,如果我在每个不同的表结果中添加数组,那么每次图片表结果首先出现时我都会得到。

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

结果:

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

如何根据与变量相关的字符串值对多维数组进行排序?

最佳答案

您想根据输入值对多维数组进行排序,有点像我们在 sql 中使用

select * from table where column like 'inputValue%'

这是一个仅基于php的解决方案

$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 文档在这里:levenshtein function

关于php - (PHP) 如何对包含与变量相关的字符串的多维数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36584600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com