php打乱数组顺序(含二维数组)

最近在做一个考试小程序的Api接口,其中一个是需要将试题列表打乱顺序,都知道,从数据库搜索出来的列表通常为数组的形式,所以我面临的问题就变成了 如何打乱数组顺序。

随手一百度,shuffle()函数 :把片段中的元素按随机顺序重新排列,这个函数是没问题的,当然会有一些注意事项,是否能解决您遇到的问题具体还请自行百度 php shuffle()函数。

显然这个函数是没能解决我的问题,我发现我查询出来的数组是二维数组,当然也有百度打乱二维数组顺序,但是结果并不理想。大多是同转自一位人的,而且第一位是错误的,剩下的转载的全是错误的。
于是自己也算是借鉴,重新写了这个方法。废话聊到这,上代码:

     //随机打乱二维数组顺序 方法
    public function shuffle_assoc($lists)
    { 
        if(!is_array($lists)){ 
            return $lists;
        }else{ 
            $keys = array_keys($lists);
            //打乱数组的键排序
            shuffle($keys);
            $result = array();
            foreach ($keys as $key){ 
                $result[$key] = $list[$key];
            }
        }
        return $result;
    }

	//测试方法中调用
	public function demo(){ 
        $data = array(
            array("id"=>1,"title"=>"语文"),
            array("id"=>2,"title"=>"数学"),
            array("id"=>3,"title"=>"英语"),
            array("id"=>4,"title"=>"物理"),
            array("id"=>5,"title"=>"化学"),
            array("id"=>6,"title"=>"生物"),
        );
        $result = $this->shuffle_assoc($data);
        var_dump($result);
        exit;
    }
    //输出结果
	array(6) { 
	  [3]=>
	  array(2) { 
	    ["id"]=>
	    int(4)
	    ["title"]=>
	    string(6) "物理"
	  }
	  [0]=>
	  array(2) { 
	    ["id"]=>
	    int(1)
	    ["title"]=>
	    string(6) "语文"
	  }
	  [4]=>
	  array(2) { 
	    ["id"]=>
	    int(5)
	    ["title"]=>
	    string(6) "化学"
	  }
	  [2]=>
	  array(2) { 
	    ["id"]=>
	    int(3)
	    ["title"]=>
	    string(6) "英语"
	  }
	  [5]=>
	  array(2) { 
	    ["id"]=>
	    int(6)
	    ["title"]=>
	    string(6) "生物"
	  }
	  [1]=>
	  array(2) { 
	    ["id"]=>
	    int(2)
	    ["title"]=>
	    string(6) "数学"
	  }
	}

上面的方法会将二维数组的键重新排序,但是还是没能解决我的问题,于是接着修改代码。(狗头:苦逼程序员) 两个方法的具体不同在于foreach循环和输出的结果,可自行将两个方法对照,不再赘述。 改进后代码:

     //随机打乱二维数组顺序 方法
    public function shuffle_assoc($lists)
    { 
        if(!is_array($lists)){ 
            return $lists;
        }else{ 
            $keys = array_keys($lists);
            //打乱数组的键排序
            shuffle($keys);
            $result = array();
            foreach ($keys as $k=> $key){ 
                $result[$k] = $list[$key];
            }
        }
        return $result;
    }
	
		//测试方法中调用
	public function demo(){ 
        $data = array(
            array("id"=>1,"title"=>"语文"),
            array("id"=>2,"title"=>"数学"),
            array("id"=>3,"title"=>"英语"),
            array("id"=>4,"title"=>"物理"),
            array("id"=>5,"title"=>"化学"),
            array("id"=>6,"title"=>"生物"),
        );
        $result = $this->shuffle_assoc($data);
        var_dump($result);
        exit;
    }
	
	    //输出结果
	    array(6) { 
		  [0]=>
		  array(2) { 
		    ["id"]=>
		    int(5)
		    ["title"]=>
		    string(6) "化学"
		  }
		  [1]=>
		  array(2) { 
		    ["id"]=>
		    int(2)
		    ["title"]=>
		    string(6) "数学"
		  }
		  [2]=>
		  array(2) { 
		    ["id"]=>
		    int(3)
		    ["title"]=>
		    string(6) "英语"
		  }
		  [3]=>
		  array(2) { 
		    ["id"]=>
		    int(4)
		    ["title"]=>
		    string(6) "物理"
		  }
		  [4]=>
		  array(2) { 
		    ["id"]=>
		    int(1)
		    ["title"]=>
		    string(6) "语文"
		  }
		  [5]=>
		  array(2) { 
		    ["id"]=>
		    int(6)
		    ["title"]=>
		    string(6) "生物"
		  }
		}

第二种是我想要的结果了,您可以根据自己的实际情况选择方法的使用。
另外多维数组的打乱顺序可以自己探索,也不是很难,这里不多说了。

本文地址:https://blog.csdn.net/liuajie1/article/details/110128679