LoginSignup
6
6

More than 5 years have passed since last update.

phpでデータベースから取得した配列を指定した順番に並び替える

Last updated at Posted at 2013-07-30

Propelを例に

<?php

// 並び順を与えられた通りに整える
$ids = array(1,3,2);
$_hoges = HogePeer::retrieveByPKs($ids);
# array(3) { [0]=> object(Hoge){["id":protected]=>1} [1]=> object(Hoge){["id":protected]=>2} [2]=> object(Hoge){["id":protected]=>3} }

$hoges = array_flip($ids);
foreach ($_hoges as $_hoge) {
    $hoges[$_hoge->getId()] = $_hoge;
}
$hoges;
# array(3) { [1]=> object(Hoge){["id":protected]=>1} [3]=> object(Hoge){["id":protected]=>3} [2]=> object(Hoge){["id":protected]=>2} }
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6