2010-01-15 163 views
2

我需要按照另一个数组内容的精确顺序排序关联数组。 该数组通过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。
希望你们中的一些人能把我带回正轨。
非常感谢提前。

+0

非常感谢所有的答案。 我尝试了它们,但SoapBox是正确的,PHP做这件事非常缓慢,因为我总是遇到内部服务器错误。 所以我去了,重新思考我的数据库表,并尝试另一种方法。 – 2010-01-15 22:40:41

回答

0

有点难以分辨,因为这里有很多事情要做,未来如果你问几个简单的问题,并找出自己如何使答案合在一起,你可能会得到更好/更多的回应。

你最好的选择是长期来重组你的SQL表,这样你可以将这些查询结合在一起。你可以在PHP中完成你所要求的工作,但这要比在MySQL中做这个要慢,而且要复杂得多。

要做到你的要求(在PHP很慢):

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

要提取您的结果:

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

$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显示它们

并提供代码提示$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); 
0

我在这种情况下倾向于做的事情是首先用数据重新排列数组。所以按键,分别代表IDS
你的情况:

$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循环。

0

编辑!

,因为我无法再编辑我的问题,我只是想说明我的解决办法是这样的:

重新考虑我的数据库是什么给我带来了一些testings,然后我找到了解决方案,具有以下尖端查询:

$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,...
有时候,这很容易,当你知道在哪里看。
再次感谢!