我需要按照另一个数组内容的精确顺序排序关联数组。 该数组通过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查询:
SQL-Ouery为$ sorting_array:
(分贝场 '的conf' 是设置为 “文本”,也许这是我的问题使我不得不先爆炸和内爆的条目之前,我可以使用它的下一个查询)
$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);
SQL-Ouery为$ array_to_sort:
$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>";
}
只带来了标识的正确的顺序,而不是内容。现在我有点困惑,因为我尝试了很多东西,但最终都给了我相同的结果。
也许sql-query可以在一个步骤中完成,但我没有使它工作。 我搜索的所有结果都只显示了如何对ASC或DESC进行排序,但不是我想要的结果。
此外,我必须承认,我是相对新的PHP和MySQL。
希望你们中的一些人能把我带回正轨。
非常感谢提前。
非常感谢所有的答案。 我尝试了它们,但SoapBox是正确的,PHP做这件事非常缓慢,因为我总是遇到内部服务器错误。 所以我去了,重新思考我的数据库表,并尝试另一种方法。 – 2010-01-15 22:40:41