Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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_Foreach - Fatal编程技术网

Php 对对象数组进行排序

Php 对对象数组进行排序,php,arrays,sorting,foreach,Php,Arrays,Sorting,Foreach,我已经阅读了一些关于这个和一般文档信息的不同主题-我相信这就是我要寻找的,但是在我的用例中数组仍然有点顽固 以下是我所拥有的: // This sets an array of values to a variable $collection_rows = get_field('collection_profiles'); 这是$collection\u行的打印: 相当糟糕 我希望按照集合\配置文件\注释的数组键/值进行排序。到目前为止,我所尝试的是: $collection_rows = g

我已经阅读了一些关于这个和一般文档信息的不同主题-我相信这就是我要寻找的,但是在我的用例中数组仍然有点顽固

以下是我所拥有的:

// This sets an array of values to a variable
$collection_rows = get_field('collection_profiles');
这是$collection\u行的打印:

相当糟糕

我希望按照集合\配置文件\注释的数组键/值进行排序。到目前为止,我所尝试的是:

$collection_rows = get_field('collection_profiles');
print_r($collection_rows);
if ($collection_rows) {
    echo '<h2>'.__('Profiles in Collection','roots').'</h2>';
    echo '<ul>';
    function cmp($a, $b) {
        if ($a->collection_profile_note == $b->collection_profile_note) {
            return 0;
        } else {
            return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1;
        }
    }
    usort($collection_rows, 'cmp');

    foreach($collection_rows as $collection_row) {
        // Extract single post value
        $collection_profile_field = $collection_row['collection_profile'];
        $collection_profile_page = isset($collection_profile_field[0]) ? $collection_profile_field[0]->ID : NULL;
        ?>
        <li><a href="<?php echo get_permalink($collection_profile_page); ?>"><?php echo get_the_title($collection_profile_page); ?></a> <?php echo $collection_row['collection_profile_note']; ?></li>
    <?php }
    echo '</ul>';
}
虽然它确实改变了显示的顺序,但没有uasort,它不会按照我想要的方式排列->使用函数2,3,1,不使用函数1,3,2


任何帮助都将不胜感激。谢谢

那么,您已经反向实现了排序逻辑:

function cmp($a, $b) {
    if ($a->collection_profile_note == $b->collection_profile_note) {
        return 0;
    } else {
        return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1;
    }
}
请注意,您正在使用以下逻辑:

如果ab 如果a>b,本质上返回-1,告诉排序函数a是否需要对阵列进行适当排序?将要排序的键和值分开,对它们进行排序,然后像这样循环遍历该数组可能更容易:

$sortme=array();
foreach( $collection_rows as $key=>$profile )
{
    $sortme[$key] = $profile['collection_profile_note'];
}

asort($sortme);

foreach( $sortme as $node=>$ignore ) 
{
    print_r($collection_rows[$node]);
}

确认这几乎使排序时间加倍,但可能足以满足您的需要?

请从浏览器的视图源复制对象数组,而不是从显示的屏幕复制。这将保留所有的换行符和空白,我们需要能够看到这一块的疯狂到底是什么。感谢您的输入-我更新了排序逻辑-但是实际的foreach语句或print\r仍然没有按升序输出带有集合配置文件注释的数组。快速粘贴:谢谢@扎克:你的排序逻辑仍然在你的粘贴箱里。啊,k最后一部分现在是返回$a->collection\u profile\u note<$b->collection\u profile\u note-1 : 1; 但是仍然没有骰子…@Zach:你的数组结构似乎也是一个数组数组,而不是一个对象数组。您需要修改cmp函数以反映这一点。注释$a->collection\u profile\u note假设$a是一个对象,而根据打印输出,它实际上是一个数组。相反,你应该比较$a['collection\u profile\u note']和$b!谢谢——这说明了为什么实现不起作用;
function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}
$sortme=array();
foreach( $collection_rows as $key=>$profile )
{
    $sortme[$key] = $profile['collection_profile_note'];
}

asort($sortme);

foreach( $sortme as $node=>$ignore ) 
{
    print_r($collection_rows[$node]);
}