PHP - 根据另一个索引的值排序数组(存储在另一个小数组中)

时间:2014-02-12 08:13:02

标签: php arrays sorting

我有这个数组:

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
        )

)

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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>';
?>

答案 2 :(得分:0)

foreach($pool_order as $order)
{
    foreach($oldArray as $ele)
    {
        if($ele->pool_id == $order)
        {
            $sortedArr[] = $ele;
        }
    }
}

仅供参考。可以优化取决于您的应用假设。

相关问题