2012-06-16 71 views
0

让我们说我有一个对象,$obj,这foreach()循环后:重新排序PHP对象

foreach($obj as $row){ 
    echo 'file_id:'.$row->file_id.'-name:'.$row->name.'<br>'; 
} 

应该是这样的:

file_id:321-name:321-is-good 
file_id:322-name:322-is-better 
file_id:323-name:323-is-best 

我怎样才能排序$obj这样我可以选择什么是第一个echo'd $row?例如,如果我在某个地方322插,结果是这样的:

file_id:322-name:322-is-better 
file_id:323-name:323-is-best 
file_id:321-name:321-is-good 

,如果我在323插入:

file_id:323-name:323-is-best 
file_id:321-name:321-is-good 
file_id:322-name:322-is-better 

我知道这可能涉及usort()但我不能换我的头脑需要做些什么。

想法?

感谢, 添

回答

2

假设你想在一个特定的开始进行排序file_id,追加前会发生什么,这也是上排序file_id

$sort = '323'; 
usort($arr,function($a,$b) use ($sort) { 
    //normal sorting for low-ids after high-ids 
    if($a->file_id < $sort && $a->file_id < $sort){ 
     return strcmp($a->file_id,$b->file_id); 
    } 
    //if only $a is smaller $a should be later 
    if($a->file_id < $sort) return 1; 
    //if only $b is smaller $a should be earlier 
    if($b->file_id < $sort) return -1; 
    //both above $sort, sort normally 
    return strcmp($a->file_id,$b->file_id); 
}); 

工作用一个例子物体代替阵列:

<?php 
//build a test with arrays is easier; 
$arr = new ArrayObject(array(
    array('file_id' => '321','name' => '321 is good'), 
    array('file_id' => '322','name' => '322 is better'), 
    array('file_id' => '323','name' => '323 is best'))); 
//cast all to object for purposes of testing objects 
foreach($arr as &$val) $val = (object)$val; 
//when you used a reference _always_ unset... trust me on this one 
unset($val); 

$sort = '323'; 
// we define the callback separately because for this test we use it more then once 
// don't pay much attention to the reference as &$sort, this is just to alter it in 
// multiple tests, and should not be needed in the actual implementation. 

$callback = function($a,$b) use (&$sort) { 
    //normal sorting for low-ids after high-ids 
    if($a->file_id < $sort && $b->file_id < $sort){ //$a & $b indeed, not $a twice ;) 
     return strcmp($a->file_id,$b->file_id); 
    } 
    //if only $a is smaller $a should be later 
    if($a->file_id < $sort) return 1; 
    //if only $b is smaller $a should be earlier 
    if($b->file_id < $sort) return -1; 
    //both above $sort, sort normally 
    return strcmp($a->file_id,$b->file_id); 
}; 

//322 first: 
$sort='322'; 
$arr->uasort($callback); 
var_dump($arr); 

//323 first: 
$sort='323'; 
$arr->uasort($callback); 
var_dump($arr); 

//321 again first: 
$sort='321'; 
$arr->uasort($callback); 
var_dump($arr); 

//non-existing: 
$sort='400'; 
$arr->uasort($callback); 
var_dump($arr); 



object(ArrayObject)#1 (1) { 
    ["storage":"ArrayObject":private]=> 
    array(3) { 
    [1]=> 
    object(stdClass)#5 (2) { 
     ["file_id"]=> 
     string(3) "322" 
     ["name"]=> 
     string(13) "322 is better" 
    } 
    [2]=> 
    object(stdClass)#6 (2) { 
     ["file_id"]=> 
     string(3) "323" 
     ["name"]=> 
     string(11) "323 is best" 
    } 
    [0]=> 
    object(stdClass)#4 (2) { 
     ["file_id"]=> 
     string(3) "321" 
     ["name"]=> 
     string(11) "321 is good" 
    } 
    } 
} 
object(ArrayObject)#1 (1) { 
    ["storage":"ArrayObject":private]=> 
    array(3) { 
    [2]=> 
    object(stdClass)#6 (2) { 
     ["file_id"]=> 
     string(3) "323" 
     ["name"]=> 
     string(11) "323 is best" 
    } 
    [0]=> 
    object(stdClass)#4 (2) { 
     ["file_id"]=> 
     string(3) "321" 
     ["name"]=> 
     string(11) "321 is good" 
    } 
    [1]=> 
    object(stdClass)#5 (2) { 
     ["file_id"]=> 
     string(3) "322" 
     ["name"]=> 
     string(13) "322 is better" 
    } 
    } 
} 
object(ArrayObject)#1 (1) { 
    ["storage":"ArrayObject":private]=> 
    array(3) { 
    [0]=> 
    object(stdClass)#4 (2) { 
     ["file_id"]=> 
     string(3) "321" 
     ["name"]=> 
     string(11) "321 is good" 
    } 
    [1]=> 
    object(stdClass)#5 (2) { 
     ["file_id"]=> 
     string(3) "322" 
     ["name"]=> 
     string(13) "322 is better" 
    } 
    [2]=> 
    object(stdClass)#6 (2) { 
     ["file_id"]=> 
     string(3) "323" 
     ["name"]=> 
     string(11) "323 is best" 
    } 
    } 
} 
object(ArrayObject)#1 (1) { 
    ["storage":"ArrayObject":private]=> 
    array(3) { 
    [0]=> 
    object(stdClass)#4 (2) { 
     ["file_id"]=> 
     string(3) "321" 
     ["name"]=> 
     string(11) "321 is good" 
    } 
    [1]=> 
    object(stdClass)#5 (2) { 
     ["file_id"]=> 
     string(3) "322" 
     ["name"]=> 
     string(13) "322 is better" 
    } 
    [2]=> 
    object(stdClass)#6 (2) { 
     ["file_id"]=> 
     string(3) "323" 
     ["name"]=> 
     string(11) "323 is best" 
    } 
    } 
} 

+0

- @ Wrikken,是的'$ sort'将在飞行中定义,谢谢你让我试试你的代码... –

+0

我没有测试过它,所以我不负责任何解析或其他错误,但它应该帮助实现这个想法;) – Wrikken

+0

- @ Wrikken,所以我需要首先将$ obj'作为$数组进行类型转换?另外,我无法返回重新订购的'$ obj'。我只是这样做:'$ obj = usort($ obj,function($ a ...'? –