开发者

PHP Reorder array of objects based on contents of another array

开发者 https://www.devze.com 2023-03-02 17:52 出处:网络
I have an array of objects that is being generated from an SQL query within a custom Joomla! 1.5 component:

I have an array of objects that is being generated from an SQL query within a custom Joomla! 1.5 component:

$query = 'SELECT * FROM #__orders_hearaboutus ORDER BY id';
$this->_hearaboutus = $this->_getList($query);

This generates something like:

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开发者_StackOverflow中文版] => Leaflet by Post
        )

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

)

This is then generating an HTML select 'Where did you hear about us' drop-down option within an order form.

What I'd like to do is to re-order the list by supplying the IDs in the desired (arbitrary) order, assuming that an array is the best way to do this:

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

I've found ways of reordering arrays in this way or reordering arrays of objects by keys but I can't work out how to achieve the above?


The most straight-forward way is to do it in SQL:

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

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field

Since it's really not good practice to use arbitrary primary keys as sort criterium, you should really add an additional order column for that purpose.


You can do this in MySQL like @deceze mentioned or in PHP, using array_multisort():

array_multisort($this->_hearaboutus, $ordering, SORT_ASC);


Doing it via MySQL as mentioned by @deceze is probably the best way of doing it, but here's a quick and dirty way of achieving what you need.

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 );
0

精彩评论

暂无评论...
验证码 换一张
取 消