Displaying and Randomising php Arrays

后端 未结 4 518
庸人自扰
庸人自扰 2021-01-22 10:04

I have an array displaying results as below:

Array
(
    [0] => 71
    [1] => 56
    [2] => 64
    [3] => 82
    [4] => 90
    [5] => 80
    [6         


        
相关标签:
4条回答
  • 2021-01-22 10:21
    $rand5 = array_rand(array_flip($show), 5);
    // we need array_flip because array_rand returns the keys
    

    http://php.net/array_rand

    http://php.net/array_flip

    0 讨论(0)
  • 2021-01-22 10:26

    Try this:

    $arr=array();
    for ($i=0;$i<5;$i++)
    {
    $arr[i]=$items[array_rand($items)];
    }
    

    $arr contains the result

    0 讨论(0)
  • 2021-01-22 10:26

    Not sure on the query that gets this, but you could also possibly use

    SELECT number
    FROM SomeTable
    ORDER BY RAND()
    LIMIT 5
    

    However note that ORDER BY RAND() is quite inefficient and as such only suitable when you are dealing with a small number of rows. However if you are bringing back a large number of rows then doing it in PHP would also be inefficient.

    There are more efficient ways of returning a single random row, so if you had a very large number of rows it could be worth using efficiently retrieving a single row 5 times and unioning the results together (although you need to take account of the possibility that the random rows could be duplicates.

    For example (and not really that happy with this):-

    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    UNION
    (SELECT r1.number
    FROM SomeTable AS r1 
    INNER JOIN 
    (
        SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
        FROM SomeTable
    ) AS r2
    ON r1.number >= r2.number
    ORDER BY r1.number ASC
    LIMIT 1)
    LIMIT 5
    

    SQL fiddle here:-

    http://sqlfiddle.com/#!2/14caa/4

    This is getting 10 random rows and then using a further LIMIT 5 to get only 5 rows. Of course there is still the small possibility that this might not get 5 rows (unlikely, but it is possible that every one of those unioned queries would return the same row)

    0 讨论(0)
  • 2021-01-22 10:45

    I would go with shuffle to randomize the arrays, as per getting the 5 elements of teh array, it really depends on your need, what do you exactly want to accomplish, getting the first 5 elements, the last ones, or some other logic.

    0 讨论(0)
提交回复
热议问题