Home  >  Article  >  Backend Development  >  新手问个很简单的PHP有关问题

新手问个很简单的PHP有关问题

WBOY
WBOYOriginal
2016-06-13 10:51:54754browse

新手问个很简单的PHP问题
一维数组

怎么随机提取3个值并且保留键值

要最效率最简单的

还有问个附加小问题 怎么随机打乱一个一维数组 并且键值不变

------解决方案--------------------

PHP code
<?phpfunction kshuffle(&$array) {    if(!is_array($array) || empty($array)) {        return false;    }    $tmp = array();    foreach($array as $key => $value) {        $tmp[] = array('k' => $key, 'v' => $value);    }    shuffle($tmp);    $array = array();    foreach($tmp as $entry) {        $array[$entry['k']] = $entry['v'];    }    return true;}<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn