Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当第二个和第三个值已知时,在PHP中对数组进行排序_Php_Arrays_Sorting_Object - Fatal编程技术网

当第二个和第三个值已知时,在PHP中对数组进行排序

当第二个和第三个值已知时,在PHP中对数组进行排序,php,arrays,sorting,object,Php,Arrays,Sorting,Object,我想对始终包含三个对象的数组进行排序 脚本知道应该返回第二个和第三个对象的名称值 如何对该数组进行排序,使其顺序正确 $categories = Array ( [48] => stdClass Object ( [term_id] => 48 [name] => Hello [term_group] => 0 [term_taxonomy_id] => 57 [taxonomy] => resume_ca

我想对始终包含三个对象的数组进行排序

脚本知道应该返回第二个和第三个对象的名称值

如何对该数组进行排序,使其顺序正确

$categories = Array ( 

[48] => stdClass Object ( 
    [term_id] => 48 
    [name] => Hello 
    [term_group] => 0 
    [term_taxonomy_id] => 57 
    [taxonomy] => resume_category 
    [description] => 
    [parent] => 0 
    [count] => 572 
    [object_id] => 18397 
    [filter] => raw 

) 

[64] => stdClass Object ( 

    [term_id] => 64 
    [name] => World 
    [term_group] => 0 
    [term_taxonomy_id] => 75 
    [taxonomy] => resume_category 
    [description] => 
    [parent] => 0 
    [count] => 22 
    [object_id] => 18397 
    [filter] => raw

)

[5] => stdClass Object ( 
    [term_id] => 5 
    [name] => Test 
    [term_group] => 0 
    [term_taxonomy_id] => 75 
    [taxonomy] => resume_category 
    [description] => 
    [parent] => 0 
    [count] => 22 
    [object_id] => 5 
    [filter] => raw  

)
我希望Hello作为第二个值,World作为最后一个值,但数组的值和顺序是动态的,并且会有所不同:

   foreach ( $categories as $category ) {
    echo $category->name;
    echo ', ';
   }

应该返回:test,hello,world

事实上,您不需要对数组进行排序,但需要重新创建它

您应该使用以下功能:

function changeArrayOrder($categories) {
    $categories = array_values($categories);
    return array (
        $categories[2],
        $categories[0],
        $categories[1],
    );
}
并使用以下命令运行它:

$categories = changeArrayOrder($categories);
现在,如果运行循环,将得到以下结果:

test, hello, world, 

定制分拣任务
usort()
能否提供一个示例数组并解释所需的排序算法?Ie:按字母顺序排列在键2上。在第一行,“hello”和“world”是小写。最后,第一个字母大写。案例对分类非常重要。除非值是“test”?
foreach($categories as$category):echo$category->name;endif应该是数组,endif应该是endForEachYou是否尝试将名称为“hello”的对象移动到位置2,将名称为“world”的对象移动到位置3?你的问题很不清楚!