-1

Is it possible to get random data from an array?

See My Array:

    
Array
(
    [0] => Array
        (
            [0] => My Data
            [1] => Airport
            [2] => Md
        )

    [1] => Array
        (
            [0] => Live in fear
            [1] => Science
            [2] => Sc
        )

    [2] => Array
        (
            [0] => State History
            [1] => Government
            [2] => MP
        )

    [3] => Array
        (
            [0] => Real Estate
            [1] => Other
            [2] => Property
        )

    [4] => Array
        (
            [0] => Real State
            [1] => Not Sure
            [2] => NoData
        )
)

I need this type of random output...

Array
(
    [0] => Array
        (
            [0] => My Data
            [1] => Airport
            [2] => Md
        )

    [1] => Array
        (
            [0] => Real State
            [1] => Not Sure
            [2] => NoData
        )

    [2] => Array
        (
            [0] => My Data
            [1] => Airport
            [2] => Md
        )

    [3] => Array
        (
            [0] => State History
            [1] => Government
            [2] => MP

        )

    [4] => Array
        (
            [0] => Live in fear
            [1] => Science
            [2] => Sc

        )
)
2
  • can't you just use shuffle Commented Aug 31, 2016 at 4:04
  • shuffle won't give you repetition (see output[0] and output[2]) Commented Aug 31, 2016 at 4:13

3 Answers 3

1

Try the following shuffle function.Hope it would help you.

function shuffle_assoc($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  shuffle($keys); 
  $random = array(); 
  foreach ($keys as $key) { 
    $random[] = $list[$key]; 
  }
  return $random; 
} 

$arr = array();
$arr[] = array('id' => 50, 'foo' => 'hello');
$arr[] = array('id' => 17, 'foo' => 'byebye');
$arr[] = array('id' => 19, 'foo' => 'foo');

print_r(shuffle_assoc($arr));
Sign up to request clarification or add additional context in comments.

Comments

0

You could simply use shuffle()

bool shuffle ( array &$array )

This function shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator that is not suitable for cryptographic purposes.

shuffle($array); // Shuffles your array keys randomly every time.

Comments

0

shuffle() will be a better option in getting out the random value from multi dimensional array.

Reference: http://php.net/manual/en/function.shuffle.php

shuffle() Example:

The shuffle() function randomizes the order of the elements in the array.

This function assigns new keys for the elements in the array. Existing keys will be removed

<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");

shuffle($my_array);
print_r($my_array);
?> 

Output:

Array ( [0] => red [1] => yellow [2] => green [3] => blue [4] => purple )
//The Output will keep shuffling if you refresh the browser.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.