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

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

WBOY
Release: 2016-06-13 13:10:12
Original
762 people have browsed it

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

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

要最效率最简单的

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

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

PHP code
<?php function 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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template