简体   繁体   中英

How to shuffle, slice and then reorder an array in php?

The code works until I try to order the slice with the asort function, I then just get a blank page when I run this. Any ideas?

<?php
$arange = range( 1 , 80, 1);

shuffle($arange);

$shufl = array_slice($arange,  0, 5);

$sortshufl = asort($shufl, SORT_NUMERIC);


foreach ($sortshufl as $number) {

    echo "$number ";
}

?>

asort returns a boolean, not the sorted array. And it's meant for associative arrays.

Use the plain sort function (also returns a boolean, sorts the array "in place").

sort($shufl, SORT_NUMERIC);
foreach ($shufl as $number) {
    echo "$number ";
}

asort() sorts the array in place and returns a boolean, so your result is in $shufl .

foreach( $shufl as $number ) {
    echo "$number ";
}

Enable PHP error reporting and you'll avoid the blank pages on errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM