2014-02-12 61 views
1

我有此數組:PHP - 排序上的其他指數的值的基礎上(存儲在另一個小陣列)陣列

Array 
(
    [0] => stdClass Object 
     (
      [question_id] => 44 
      [question_name] => how to call javascript function in php 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 1 
      [pool_id] => 4 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

    [1] => stdClass Object 
     (
      [question_id] => 42 
      [question_name] => how to call recursive function 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 1 
      [pool_id] => 4 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

    [2] => stdClass Object 
     (
      [question_id] => 5 
      [question_name] => What is the correct way to end a PHP statement? 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 10 
      [pool_id] => 2 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

    [3] => stdClass Object 
     (
      [question_id] => 32 
      [question_name] => Who invented Copy, Paste? 
      [question_type] => Multiple choice 
      [ttype_id] => 1 
      [marks] => 5 
      [pool_id] => 1 
      [deleted] => 0 
      [deleted_by] => 1 
     ) 

    [4] => stdClass Object 
     (
      [question_id] => 32 
      [question_name] => Who invented Copy, Paste? 
      [question_type] => Multiple choice 
      [ttype_id] => 1 
      [marks] => 5 
      [pool_id] => 1 
      [deleted] => 0 
      [deleted_by] => 1 
     ) 

    [5] => stdClass Object 
     (
      [question_id] => 42 
      [question_name] => how to call recursive function 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 1 
      [pool_id] => 4 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

) 

我需要通過存儲在另一個數組作爲pool_id序列重新安排它:

$pool_order = array(1,2,3,4,5); 

以便按此順序重新排列此數組。任何幫助?

我想這種安排:

Array 
(

    [0] => stdClass Object 
     (
      [question_id] => 32 
      [question_name] => Who invented Copy, Paste? 
      [question_type] => Multiple choice 
      [ttype_id] => 1 
      [marks] => 5 
      [pool_id] => 1 
      [deleted] => 0 
      [deleted_by] => 1 
     ) 

    [1] => stdClass Object 
     (
      [question_id] => 32 
      [question_name] => Who invented Copy, Paste? 
      [question_type] => Multiple choice 
      [ttype_id] => 1 
      [marks] => 5 
      [pool_id] => 1 
      [deleted] => 0 
      [deleted_by] => 1 
     ) 

    [2] => stdClass Object 
     (
      [question_id] => 5 
      [question_name] => What is the correct way to end a PHP statement? 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 10 
      [pool_id] => 2 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

    [3] => stdClass Object 
     (
      [question_id] => 44 
      [question_name] => how to call javascript function in php 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 1 
      [pool_id] => 4 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

    [4] => stdClass Object 
     (
      [question_id] => 42 
      [question_name] => how to call recursive function 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 1 
      [pool_id] => 4 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

    [5] => stdClass Object 
     (
      [question_id] => 42 
      [question_name] => how to call recursive function 
      [question_type] => Single choice 
      [ttype_id] => 1 
      [marks] => 1 
      [pool_id] => 4 
      [deleted] => 0 
      [deleted_by] => 0 
     ) 

) 
+0

你做了什麼@Ali? –

+1

http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value我認爲這是重複的 – user1767754

+0

你想要一個自定義的訂單,或只是一個desc或asc命令? –

回答

1

試試這個

// assuming $your_object_array is your main array 

$your_new_array = array(); 
$pool_order = array(1,2,3,4,5); 

foreach($pool_order as $order) 
{ 
    foreach($your_object_array as $key=>$arr) 
    { 
     if($order==$arr->pool_id) 
     { 
      $your_new_array[] = $arr; 
      unset($your_object_array[$key]); 
     } 
    } 
} 

print_r($your_new_array); 
+0

工作:)謝謝 – Ali

+0

@Ali接受這個,如果這對你有用 – 2014-02-12 09:44:29

+0

哦,我忘了,完成;) – Ali

0

你試過在array_multisort()?好像你正在尋找的功能: http://www.php.net/manual/en/function.array-multisort.php

<?php 
$array[] = array(
    'key1'  => 1, 
    'dummy_data' => 2, 
    'pool_id'  => 3 
); 

$array[] = array(
    'key1'  => 5, 
    'dummy_data' => 7, 
    'pool_id'  => 2 
); 

$array[] = array(
    'key1'  => 2, 
    'dummy_data' => 4, 
    'pool_id'  => 1 
); 

foreach ($array as $entry) { 
    $pool_ids[] = $entry['pool_id']; 
} 
array_multisort($pool_ids, SORT_ASC, SORT_STRING, $array); 

echo '<pre>'; 
print_r($array); 
echo '</pre>'; 
?> 
0
foreach($pool_order as $order) 
{ 
    foreach($oldArray as $ele) 
    { 
     if($ele->pool_id == $order) 
     { 
      $sortedArr[] = $ele; 
     } 
    } 
} 

僅供參考。可以優化取決於您的應用程序假設。