【问题标题】:(PHP) How to order a multi dimensonal array with strings related to a variable?(PHP) 如何使用与变量相关的字符串对多维数组进行排序?
【发布时间】:2016-08-03 17:37:48
【问题描述】:

嘿,我的网站上有一个新功能,您可以在其中 通过在搜索输入中输入一些关键字来搜索 ya 项目。当然,我正在使用 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 查询中执行 ORDER BY?

标签: php mysql arrays ajax


【解决方案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

【讨论】:

  • 到目前为止工作正常,明天我会看到更接近您的代码并更好地了解您到底做了什么。
【解决方案2】:

array_multisort 可以帮忙吗?您是否打算返回仅以“m”开头的结果?

<?php
// create an array of the "column" to sort on...
foreach ($returnData as $row)  
{
    $test[] = $row[2];
}

// sort it...
array_multisort($test, SORT_DESC, $returnData);
?>

【讨论】:

  • 不,你可以看到它会首先响应以另一个字母开头的项目。
【解决方案3】:

您的问题有两种可能的解决方案:

1. SQL 解决方案

让数据库使用联合为您进行排序。例如:

SELECT
   id1, 
   id2,
   name
FROM
   (
       (SELECT id1, id2, name FROM pictures WHERE something=... ORDER BY name LIMIT 5)
       UNION ALL
       (SELECT id1, id2, name FROM videos WHERE something=... ORDER BY name LIMIT 5)
    ) AS q
ORDER BY
   name

2。 PHP解决方案

您正在进行非标准排序,因此您需要使用usort,它采用比较器函数来比较两个数组项:

$allData = array_merge($sql_picture, $sql_video, ...);
usort($allData, function($a, $b) { return strcmp($a[2], $b[2]); });

【讨论】:

  • 我也想合并我的 4 表 sql 请求,但我没有做到。所以我暂时建立了一些别的东西来以另一种方式达到我的目标。我是怎么做到的?
  • 因为真的我没有得到你在回答中写的语法。它看起来像正常的请求一样复杂。
  • @DOCTYPEHTML 我稍微编辑了它以显示您放置 WHERE 子句的位置。我不知道您的字段名称,但将 id1 和 id2 更改为您数据库中的任何内容。
  • 只有两个表请求我是怎么回事。添加更多?
  • 添加更多 UNION ALL (SELECT ... )
猜你喜欢
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多