1

I have this array:

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
        )

)

And I need to rearrange it by pool_id sequence stored in another array as:

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

So that this array is rearranged in this order. Any help?

I want this arrangement:

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
        )

)
Ali
  • 5,021
  • 4
  • 26
  • 45

3 Answers3

1

try this

// 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

Have you tried array_multisort()? It seems like the function that you are looking for: 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>';
?>
Christoffer Winterkvist
  • 3,074
  • 2
  • 19
  • 14
0
foreach($pool_order as $order)
{
    foreach($oldArray as $ele)
    {
        if($ele->pool_id == $order)
        {
            $sortedArr[] = $ele;
        }
    }
}

Just for reference. Can be optimize depends on your application assumption.