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

Php 下面模式中的数组排序

Php 下面模式中的数组排序,php,arrays,Php,Arrays,我有下面的数组 Array ( [0] => Array ( [ft_name] => workout [days] => 2 ) [1] => Array ( [ft_name] => restday [days] => ) [2] => Array

我有下面的数组

Array
(
    [0] => Array
        (
            [ft_name] => workout
            [days] => 2
        )

    [1] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [2] => Array
        (
            [ft_name] => df
            [days] => 3
        )

    [3] => Array
        (
            [ft_name] => df
            [days] => 1
        )

    [4] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [5] => Array
        (
            [ft_name] => ss
            [days] => 6
        )

    [6] => Array
        (
            [ft_name] => reday
            [days] => 5
        )

)
我希望它采用这种格式,就像数组的位置有
ft\u name=>“restday”
应保持不变,其余应按<代码>天数字段按顺序排序

我已尝试使用此代码,但结果不是预期的

<?php    
$size = count($val);
    for($i=0;$i< $size;$i++){
        if($val[$i]['days']){
            for ($j=0; $j<$size-$i; $j++) {

                if(!$val[$j+1]['days']) continue;   

                if ($val[$j+1]['days'] < $val[$j]['days']) {
                      swap($val, $j, $j+1);
                }
            }
        }
        else{
            continue;
        }

    }
    echo "<pre>"; print_r($val);

    function swap(&$arr, $a, $b) {
        $tmp = $arr[$a];
        $arr[$a] = $arr[$b];
        $arr[$b] = $tmp;
    }
?>

提前感谢。

您应该尝试
阵列\u多端口
,这里是



你有一篇关于PHP排序数组的好文章

你应该试试
array\u multisort
,这里有


您有一篇关于PHP排序数组的文章,它与回调函数一起使用,回调函数有两个测试,一个是主字段,如果主字段的值相等,则测试第二个字段

您可以投票否决我误解的内容

使用具有两个测试的回调函数,一个测试主字段,如果主字段的值相等,则测试第二个字段


你可以投票否决我,我误解了

这不是你知道的免费服务代码,那么你试过什么?你试过
array\u multisort
吗?试过
array\u multisort
@Darren我已经添加了代码,请检查。这不是你知道的免费服务代码,那么你尝试过什么呢?你尝试过
array\u multisort
?尝试过
array\u multisort
@Darren我已经添加了代码,请检查。谢谢,我已经尝试过这个代码,但是它也改变了restday的位置,我想保持restday的位置不变。你可以尝试在foreach中添加if条件,我会更新答案谢谢,我已经尝试过这个代码,但是它也改变了restday的位置,我想保持restday的位置不变。你可以尝试在foreach中添加if条件,我会更新答案
Array
(
    [0] => Array
        (
            [ft_name] => df
            [days] => 1
        )

    [1] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [2] => Array
        (
            [ft_name] => workout
            [days] => 2
        )

    [3] => Array
        (
            [ft_name] => df
            [days] => 4
        )        

    [4] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [5] => Array
        (
            [ft_name] => reday
            [days] => 5
        )

    [6] => Array
        (
            [ft_name] => ss
            [days] => 6
        )

)
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    if ( $row['days'] != '' ){
        $days[$key]  = $row['days'];
        $name[$key] = $row['ft_name']; 
    }else{
        $days[$key] = $key; 
        $name[$key] = $row['ft_name']; 
        }
    }

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($days, SORT_ASC, $name, SORT_ASC, $data);
?>