PHP按其他数组对关联数组排序

PHP按其他数组对关联数组排序,php,arrays,associative-array,Php,Arrays,Associative Array,我需要按照另一个数组内容的确切顺序对关联数组进行排序。 数组由两个单独的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 o

我需要按照另一个数组内容的确切顺序对关联数组进行排序。 数组由两个单独的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:
(数据库字段'conf'设置为“text”,这可能是我的问题,因此我必须先分解并内爆条目,然后才能将其用于下一个查询。)

$array\u to\u排序的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\u进行如下排序以逐个查看内容:
(如果下面的行与上面的数组不匹配,那么我就把它弄混了。但是,下面的行是内容的来源)

但它是按“id”ASC排序的,但我需要的排序与$sorting\u数组中的排序完全相同。 我用:

while(list(,$array_to_sort) = each($sorting_array)){
$i++;
echo $array_to_sort . "<br>";
}
while(列表(,$array\u to\u sort)=每个($sorting\u array)){
$i++;
echo$array_to_sort.“
”; }
这只会使Id的顺序正确,但不会使内容正确。现在我有点困惑,因为我尝试了很多东西,但最终都得到了相同的结果。
也许sql查询可以一步完成,但我没有让它工作。 我搜索的所有结果只是显示了如何对ASC或DESC进行排序,但不是我想要的。

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

非常感谢。

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

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

要执行您要求的操作(在PHP中非常慢):

要获取结果,请执行以下操作:

$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 ] );
}
$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);
对于代码获取
$sorting\u array
,还有一个额外的提示:

foreach ( $sorting_array as $id ) {
  // replace the print_r with your display code here
  print_r( $array_to_sort[ $id ] );
}
$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);
$result=mysql\u查询(“从配置中选择conf,其中user='me'”,$dbi);
$conf=mysql\u fetch\u数组($result,$dbi);
$temp=explode(“,”,$conf[0]);
//最多30个ID
$new=array();
//无需手动执行此操作,请使用循环
对于($i=0;$i<30;++$i)
$new[]=$temp[0];
$sorting_array=内爆(',',$new);

在这种情况下,我倾向于首先用数据重新排列数组。因此,这些键表示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];
}
此解决方案非常有效,因为您只有两个foreach循环。

编辑

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

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

$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,…
有时候,当你知道该去哪里找的时候,事情就这么简单了。

再次感谢

谢谢你的回答。我试过了,但SoapBox是对的,PHP做这件事非常慢,因为我总是遇到一个内部服务器错误。所以我去重新思考我的数据库表,并尝试另一种方法。
ORDER BY FIELD(id,$sorting_array)