【问题标题】:PHP Sorting associative-array by other arrayPHP按其他数组排序关联数组
【发布时间】:2010-01-15 16:54:59
【问题描述】:

我需要按照另一个数组内容的确切顺序对关联数组进行排序。 数组由 2 个单独的 sql 请求(如下所述)检索。请求不能合并为一个请求,所以我必须将第二个数组按第一个数组的顺序排序。

这些是数组:

#Array which contains the id's in needed order
$sorting_array = array(1,2,3,8,5,6,7,9,11,10...);

#Array which contains the values for the id's, but in order of "id" ASC
$array_to_sort = array(
              array("id" => "1", "name" => "text1", "help" => "helptxt2");
              array("id" => "2", "name" => "text2", "help" => "helptxt2");
);

SQL 查询:
$sorting_array 的 SQL-Ouery:
(db-field 'conf' 设置为“text”,也许这是我的问题,所以我必须先分解和内爆条目,然后才能将其用于下一个查询.)

$result = sql_query("select conf from config where user='me'", $dbi);
$conf = sql_fetch_array($result, $dbi);
$temp = explode(',', $conf[0]); 
$new = array($temp[0], $temp[1], $temp[2], $temp[3],$temp[4],
             $temp[5], $temp[6], $temp[7], $temp[8], $temp[9],
             $temp[10], ...);#Array has max 30 entries, so I count them down here
$sorting_array = implode(',', $new);

$array_to_sort 的 SQL-Ouery:

$result = sql_query("SELECT id, name, helptxt
                   FROM table 
                   WHERE id IN ($sorting_array)
                   AND language='english'"); 
while ($array_to_sort[] = mysql_fetch_array ($result, MYSQL_ASSOC)) {}
array_pop($array_to_sort);#deleting the last null entry

我可以通过如下方式访问$array_to_sort来一一查看内容:
(如果下面的行与上面的数组不匹配,那我就把它弄混了。但是,下面的行是带来内容的)

echo $array_to_sort[0]["id"];
echo $array_to_sort[0]["name"];
echo $array_to_sort[0]["helptxt"];

但它是按“id”ASC 排序的,但我需要完全按照 $sorting_array 中的排序。 我尝试了一些方法:

while(list(,$array_to_sort) = each($sorting_array)){
$i++;
echo $array_to_sort . "<br>";
}

这只会使 Id 以正确的顺序排列,而不是内容。现在我有点困惑,因为我尝试了很多东西,但最终都给了我相同的结果。
也许 sql-query 可以一步完成,但我没有把它带到工作中。 我的所有搜索结果都显示了如何对 ASC 或 DESC 进行排序,但不是我想要的。

此外,我必须承认我对 PHP 和 MySQL 还比较陌生。
希望你们中的某个人能让我重回正轨。
非常感谢。

【问题讨论】:

  • 非常感谢所有的答案。我试过了,但 SoapBox 是对的,PHP 这样做很慢,因为我总是遇到内部服务器错误。所以我去重新考虑我的数据库表并尝试另一种方法。

标签: php arrays associative-array


【解决方案1】:

获取结果:

$result = mysql_query("SELECT id, name, helptxt
  FROM table 
  WHERE id IN ($sorting_array)
  AND language='english'");
$array_to_sort = array();
while ( ($row = mysql_fetch_assoc($result)) !== false ) {
  // associate the row array with its id
  $array_to_sort[ $row[ "id" ] ] = $row;
}

$sorting_array的顺序显示它们:

foreach ( $sorting_array as $id ) {
  // replace the print_r with your display code here
  print_r( $array_to_sort[ $id ] );
}

还有一个关于获取$sorting_array的代码的额外提示:

$result = mysql_query("select conf from config where user='me'", $dbi);
$conf = mysql_fetch_array($result, $dbi);
$temp = explode(',', $conf[0]);
// limit to 30 ids
$new = array();
// no need to do this manually, use a loop
for ( $i = 0; $i < 30; ++$i )
  $new[] = $temp[ 0 ];
$sorting_array = implode(',', $new);

【讨论】:

    【解决方案2】:

    这有点难说,因为这里发生了很多事情,将来如果你问几个简单的问题并弄清楚如何让答案结合在一起,你可能会得到更好/更多的回答。

    从长远来看,您最好的选择是重组您的 SQL 表,以便您可以将这些查询组合在一起。你可以用 PHP 做你想要做的事情,但它会比在 MySQL 中做的要慢,而且要复杂得多。

    按照您的要求做(PHP 相当慢):

    $sorted = array();
    foreach ( $sorting_array as $id )
    {
        foreach ( $array_to_sort as $values )
        {
             if ( $values['id'] == $id )
             {
                $sorted[] = $values;
                break;
             }
        }
    }
    

    【讨论】:

      【解决方案3】:

      在这种情况下,我倾向于首先用数据重新排列数组。所以键代表ID
      在你的情况下:

      $array_to_sort_ids = array();
      
      foreach ($array_to_sor as $item)
      {
          $array_to_sort_ids[$item['id']] = $item;
      }
      

      那么排序就这么简单:

      $array_sorted = array();
      
      foreach ($sorting_array as $id)
      {
          $array_sorted[] = $array_to_sort_ids[$id];
      }
      

      此解决方案非常有效,因为您只有 2 个 foreach 循环。

      【讨论】:

        【解决方案4】:

        编辑!!!

        由于我无法再编辑我的问题,我只想这样陈述我的解决方案:

        重新考虑我的数据库的提示是什么让我进行了一些测试,然后我找到了解决方案,并使用以下查询:

        $result = sql_query("SELECT id, name, helptxt
                       FROM table 
                       WHERE id IN ($sorting_array)
                       AND language='english'
                       ORDER BY FIELD(id,$sorting_array)"); 
        while ($array_to_sort[] = mysql_fetch_array ($result, MYSQL_ASSOC)) {}
        array_pop($array_to_sort);#deleting the last null entry
        

        只要一行:

        ORDER BY FIELD(id,$sorting_array)
        

        会成功的。它以您想要的方式对结果进行排序,即使这意味着 1,4,2,3,9,7,...
        有时,当您知道去哪里寻找时,这很容易。
        再次感谢!!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-26
          • 2011-01-28
          • 1970-01-01
          • 2019-02-21
          • 2012-08-25
          • 2019-04-02
          • 2021-07-07
          相关资源
          最近更新 更多