Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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_Sorting_Ksort - Fatal编程技术网

Php 按输入字符串和位置自定义数组排序

Php 按输入字符串和位置自定义数组排序,php,sorting,ksort,Php,Sorting,Ksort,我有一个数组。它看起来像: $arr = [ '0' => ['id'=>9, 'q'=>'motor', 'pos'=>1], '1' => ['id'=>10, 'q'=>'NULL', 'pos'=>0], '2' => ['id'=>7, 'q'=>'motor', 'pos'=>2], '3' => ['id'=>8, 'q'=>'NULL', 'pos'=>0],

我有一个数组。它看起来像:

$arr = [
   '0' => ['id'=>9, 'q'=>'motor', 'pos'=>1],
   '1' => ['id'=>10, 'q'=>'NULL', 'pos'=>0],
   '2' => ['id'=>7, 'q'=>'motor', 'pos'=>2],
   '3' => ['id'=>8, 'q'=>'NULL',  'pos'=>0],
   '4' => ['id'=>11, 'q'=>'motor','pos'=>3],
   '5' => ['id'=>11, 'q'=>'exhaust','pos'=>1]
];
如何对上面的数据进行排序,使其看起来像(如果q='motor'在搜索字符串中):

因此:

谢谢

更新(问题/答案) p、 s:我试图使用自定义变量作为输入:$q


并且该功能停止工作。如何解决此问题?

您可以使用
usort

usort($arr, function($a, $b) {
    if ($a['q'] == 'motor') {
        if($b['q'] == 'motor') {
            return ($a['pos'] > $b['pos']) ? 1 : -1;
        }
        return -1;
    }
    if ($b['q'] == 'motor') {
        return 1;
    }
    return 0;
});

如果您需要不同的顺序,只需更改功能以满足您的需要。

U sort将在这里帮助您

usort( $arr, function ( $a, $b ) {
    if ( $a['q'] === $b['q'] ) {
        return $a['pos'] < $b['pos'] ? -1 : 1;
    }
    if ( $a['q'] === 'motor' ) {
        return -1;
    }
    if ( $b['q'] === 'motor' ) {
        return 1;
    }
    return 0;
} );
usort($arr,function($a,$b){
如果($a['q']==$b['q']){
返回$a['pos']<$b['pos']?-1:1;
}
如果($a['q']='motor'){
返回-1;
}
如果($b['q']='motor'){
返回1;
}
返回0;
} );

这里有一个
usort
,可以处理您的需求,并封装在一个函数中,该函数可以在适当的位置转换阵列:

  • 带有
    q
    键且包含
    $query
    的项目将放置在数组的前面

  • 带有
    q
    键且包含
    $query
    的项目将按其
    pos
    值升序排序

  • 其他所有内容都将按其
    pos
    值排序


  • 在没有提及的情况下修改别人的答案不是很粗鲁吗?@Rafael,我们大约在同一时间发布了这篇文章。谢谢你假设我偷了他的答案,尽管我没有。他没有按照OP的要求解释“pos”。@Ice76,我假设是因为变量和返回,但好的,一方不能假设另一方在没有证据的情况下窃取了答案,我道歉。答案并没有提供OP想要的东西。结果不是作为他的样本订购的。做了更改后,当
    $b
    是一个电机时,我犯了返回-1的错误。我修复了它,现在它返回到我最初预期的状态——显然每个人都喜欢$a、$b变量。:)其他元素的位置是否正确!=是否应考虑使用电机?是。非常感谢。很好,但是一开始的索引ID被重置了。如果他们对这项任务无关紧要,我会考虑回答。谢谢!我试着使用这样的东西:usort($arr,function($a,$b)use($q){->并将“motor”替换为$q。上面的函数->停止工作。
    usort($arr, function($a, $b) use ($q) {
            if ($a['q'] == $q) {
                if($b['q'] == $q) {
                    return ($a['pos'] > $b['pos']) ? 1 : -1;
                }
                return -1;
            }
            if ($b['q'] == $q) {
                return 1;
            }
            return 0;
        });
    
    usort($arr, function($a, $b) {
        if ($a['q'] == 'motor') {
            if($b['q'] == 'motor') {
                return ($a['pos'] > $b['pos']) ? 1 : -1;
            }
            return -1;
        }
        if ($b['q'] == 'motor') {
            return 1;
        }
        return 0;
    });
    
    usort( $arr, function ( $a, $b ) {
        if ( $a['q'] === $b['q'] ) {
            return $a['pos'] < $b['pos'] ? -1 : 1;
        }
        if ( $a['q'] === 'motor' ) {
            return -1;
        }
        if ( $b['q'] === 'motor' ) {
            return 1;
        }
        return 0;
    } );
    
    function custom_sort($query, &$arr) {
        usort($arr, function($a, $b) use ($query) {
            $in_a = strpos($a['q'], $query) !== false;
            $in_b = strpos($b['q'], $query) !== false;
    
            if ($in_a && !$in_b) {
                return -1;
            }
            if (!$in_a && $in_b) {
                return 1;
            }
    
            return $a['pos'] - $b['pos'];
        });
    }
    
    custom_sort("motor", $arr);