Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.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:调用时函数未运行,返回1_Php - Fatal编程技术网

PHP:调用时函数未运行,返回1

PHP:调用时函数未运行,返回1,php,Php,因此,我制作了一个php函数,用于按姓氏和名字对一些用户数据进行排序。我完美地分割了数据,但当我尝试使用排序函数时,它只返回1,而不是返回数组。我甚至把print语句放在函数中,看它是否被执行,它们是否从未被调用,所以我很困惑。谢谢你的帮助 <?php //Enter your code here, enjoy! $data = "email:billy@poop.co;first:zilly;last:bobjoe;time:2005-03-23,email:b

因此,我制作了一个php函数,用于按姓氏和名字对一些用户数据进行排序。我完美地分割了数据,但当我尝试使用排序函数时,它只返回1,而不是返回数组。我甚至把print语句放在函数中,看它是否被执行,它们是否从未被调用,所以我很困惑。谢谢你的帮助

    <?php
        //Enter your code here, enjoy!

$data = "email:billy@poop.co;first:zilly;last:bobjoe;time:2005-03-23,email:bidlly@poop.co;first:brilly;last:dobjoe;time:2005-03-23,email:billy@poop.co;first:billy;last:bobjoeee;time:2005-03-23,email:billy@poop.co;first:bilssly;last:killer;time:2005-03-23,email:billy@poop.co;first:dilly;last:bobjoe;time:2005-03-23,email:billy@poop.co;first:billyddd;last:aero;time:2005-03-23";
$data_split = explode(",", $data);
//email:billy@poop.co;first:zilly;last:bobjoe
// EMAIL FIRST LAST
// array_push(arr, item)
$data_sorted = [];
foreach ($data_split as $user){
    $user_info = explode(";", $user);
    $this_user_data = [];
    foreach ($user_info as $user_piece) {
        $split_item = explode(":", $user_piece);
        $this_user_data[$split_item[0]] = $split_item[1];
    }
    array_push($data_sorted, $this_user_data);
}
// sort by last name then first name
if (!function_exists("sort")){
function sort($arr_unsorted) {
    print_r("hello");
    if (count($arr_unsorted) <= 1) {
        return $arr_unsorted;
    }
    print_r("hello?");
    $arr = $arr_unsorted;
    $swaps = 0;
    do {
        $swaps = 0;
    for ($i=0;$i<count($arr) - 1;$i+=1){
        // 0 , 1 compare... if (a,b)->-1  (b,a) -> +1 (b,b) -> 0
        $result = strcasecmp($arr[$i][last], $arr[$i + 1][last]);

        if ($result > 0){
            $temp = $arr[$i];
            $arr[$i] = $arr[$i + 1];
            $arr[$i + 1] = $temp;
            $swaps += 1;
            print_r("swap");
        }
        else if ($result == 0){
            $result_first = strcasecmp($arr[$i][first], $arr[$i + 1][first]);
            if ($result_first > 0) {
                $temp = $arr[$i];
                $arr[$i] = $arr[$i + 1];
                $arr[$i + 1] = $temp;
                $swaps += 1;
                print_r("swap");
            }
        }

    }
    } while ($swaps != 0);

    return $arr;
}
}
print_r($data_sorted);
$new_arr = sort($data_sorted);
print_r($new_arr);


再次感谢您明天提供的帮助

PHP还有一个名为的函数,因此您的自定义排序函数尚未定义,您使用的是返回布尔值的PHP排序函数。尝试将您的排序函数更改为其他名称。yoooo您是一个传奇,它就是这样。非常感谢。还可以考虑将其放在自己的类中,然后您可以定义“sort\u last\u then\u first”函数以及“main”或“index”函数
Array
(
    [0] => Array
        (
            [email] => billy@poop.co
            [first] => zilly
            [last] => bobjoe
            [time] => 2005-03-23
        )

    [1] => Array
        (
            [email] => bidlly@poop.co
            [first] => brilly
            [last] => dobjoe
            [time] => 2005-03-23
        )

    [2] => Array
        (
            [email] => billy@poop.co
            [first] => billy
            [last] => bobjoeee
            [time] => 2005-03-23
        )

    [3] => Array
        (
            [email] => billy@poop.co
            [first] => bilssly
            [last] => killer
            [time] => 2005-03-23
        )

    [4] => Array
        (
            [email] => billy@poop.co
            [first] => dilly
            [last] => bobjoe
            [time] => 2005-03-23
        )

    [5] => Array
        (
            [email] => billy@poop.co
            [first] => billyddd
            [last] => aero
            [time] => 2005-03-23
        )

)
1