php, how to jumble / randomize order of associative array while keeping key/value pairs

前端 未结 2 533
不知归路
不知归路 2021-01-21 02:24

what is the php function to randomize the associative array while keeping key/values pairs. I don\'t mean to just randomly pick out a key value pair, but actually changing the

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 02:48

    You could use shuffle() on array_keys, then loop around your array adding them to the list in the new order.

    E.g.

    $shuffleKeys = array_keys($array);
    shuffle($shuffleKeys);
    $newArray = array();
    foreach($shuffleKeys as $key) {
        $newArray[$key] = $array[$key];
    }
    

提交回复
热议问题