如何洗牌PHP中的数组,同时还知道原始指数? [英] How to shuffle an array in PHP while still knowing the original index?

查看:94
本文介绍了如何洗牌PHP中的数组,同时还知道原始指数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么洗牌PHP数组,同时还知道原始指数?

How do you shuffle a PHP array while still knowing the original index?

我在PHP一个简单的数组(例如 [A,B,C,[5,10] )。

I have a simple array in PHP (e.g. ["a", "b", "c", [5, 10]]).

洗牌()功能可让您洗牌的元素。

The shuffle() function allows you to shuffle the elements.

不过,我仍想知道什么是原来的指数。我怎样才能做到这一点?

However I still wish know what the original index was. How can I do this?

推荐答案

请参见洗牌文档示例2:

<?php
    function shuffle_assoc(&$array) {
        $keys = array_keys($array);

        shuffle($keys);

        foreach($keys as $key) {
            $new[$key] = $array[$key];
        }

        $array = $new;

        return true;
    }

这篇关于如何洗牌PHP中的数组,同时还知道原始指数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆