PHP根据另一个数组的内容对对象数组重新排序

PHP根据另一个数组的内容对对象数组重新排序,php,joomla1.5,sorting,Php,Joomla1.5,Sorting,我有一个从定制Joomla中的SQL查询生成的对象数组!1.5组成部分: $query = 'SELECT * FROM #__orders_hearaboutus ORDER BY id'; $this->_hearaboutus = $this->_getList($query); 这会产生如下结果: Array ( [0] => stdClass Object ( [id] => 3 [how

我有一个从定制Joomla中的SQL查询生成的对象数组!1.5组成部分:

$query = 'SELECT * FROM #__orders_hearaboutus ORDER BY id';
$this->_hearaboutus = $this->_getList($query);
这会产生如下结果:

Array
(
    [0] => stdClass Object
        (
            [id] => 3
            [how_heard] => Our Website
        )

    [1] => stdClass Object
        (
            [id] => 4
            [how_heard] => Other Website
        )

    [2] => stdClass Object
        (
            [id] => 5
            [how_heard] => Word of Mouth
        )

    [3] => stdClass Object
        (
            [id] => 6
            [how_heard] => Other
        )

    [4] => stdClass Object
        (
            [id] => 10
            [how_heard] => Internet Search Engine
        )

    [5] => stdClass Object
        (
            [id] => 11
            [how_heard] => Local Newspaper
        )

    [10] => stdClass Object
        (
            [id] => 16
            [how_heard] => Leaflet by Post
        )

    [11] => stdClass Object
        (
            [id] => 18
            [how_heard] => Club or Society Newsletter
        )

)
然后,这将在订单中生成一个HTML选择“您在哪里听说过我们”下拉选项

我想做的是通过按所需(任意)顺序提供ID来重新排列列表,假设数组是最好的方式:

$ordering = array(11,3,4,10,16,5,18,6);

我已经找到了用这种方式对数组重新排序或按键对对象数组重新排序的方法,但我不知道如何实现上述目的?

最直接的方法是用SQL:

$query = 'SELECT * FROM ... ORDER BY FIELD(id,11,3,4,10,16,5,18,6)';


由于使用任意主键作为排序标准并不是一种好的做法,因此您应该为此添加一个额外的
order
列。

最直接的方法是使用SQL:

$query = 'SELECT * FROM ... ORDER BY FIELD(id,11,3,4,10,16,5,18,6)';


由于使用任意主键作为排序标准不是很好的做法,因此您应该为此添加一个额外的
order


您可以在MySQL中(如前面提到的@deceze)或PHP中使用以下命令执行此操作:


正如@deceze所提到的,通过MySQL来实现这一点可能是最好的方式,但这里有一种快速而肮脏的方式来实现您所需要的

class testObj {
    public $id;
    function __construct($id) {
        $this->id = $id;
    }
}

$order = array( 11, 3, 4, 10, 16, 5, 18, 6);
$objects = array(
    new testObj(3),
    new testObj(4),
    new testObj(5),
    new testObj(6),
    new testObj(10),
    new testObj(11),
    new testObj(16),
    new testObj(18)
);

$neworder = array();

foreach ( $order as $ord ) {

    foreach ( $objects as $obj ) {

        if ( $obj->id == $ord ) {
            $neworder[] = $ord;
        }

    }

}

print_r( $neworder );

正如@deceze所提到的,通过MySQL来实现这一点可能是最好的方式,但这里有一种快速而肮脏的方式来实现您所需要的

class testObj {
    public $id;
    function __construct($id) {
        $this->id = $id;
    }
}

$order = array( 11, 3, 4, 10, 16, 5, 18, 6);
$objects = array(
    new testObj(3),
    new testObj(4),
    new testObj(5),
    new testObj(6),
    new testObj(10),
    new testObj(11),
    new testObj(16),
    new testObj(18)
);

$neworder = array();

foreach ( $order as $ord ) {

    foreach ( $objects as $obj ) {

        if ( $obj->id == $ord ) {
            $neworder[] = $ord;
        }

    }

}

print_r( $neworder );