Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I got this array:

**Array 1 Example:**
Array
        (
            [0] => 62
            [1] => 9
            [2] => Array
                (
                    [0] => 5
                    [1] => 16
                    [2] => 45
                )

            [3] => Array
                (
                    [0] => 11
                    [1] => 21
                    [2] => 25
                    [3] => 32
                    [4] => 50
                )

            [4] => Array
                (
                    [0] => 4
                    [1] => 23
                    [2] => 37
                    [3] => 57
                )

            [5] => Array
                (
                    [0] => 13
                    [1] => 15
                    [2] => 18
                    [3] => 22
                    [4] => 27
                    [5] => 30
                )

        )

My problem is, i can't get a random value out of #Array 1 Example. So i tried to collect all the values, in a new array, so it would look like #Array 2 Example, but making that array seems almost impossible for me.

**Array 2 Example:**

Array
        (
            [0] => 62
            [1] => 56
            [2] => 16
            [3] => 44
            [4] => 54
            [5] => 11
            [6] => 21
            [7] => 25
            [8] => 32
            [9] => 33
            [10] => 4
            [11] => 12
            [12] => 23
            [13] => 57
            [14] => 13
            [15] => 15
            [16] => 18
            [17] => 22
            [18] => 27
            [19] => 30
        )

I tried all sort of code, but i can't get anything to work.

Any suggestions?

Thanks!

share|improve this question
    
What code have you tried? "All sort" is broad – Robin Carlier Nov 5 '14 at 19:16
    
It's not even clear what you're trying to do... at first I thought you just wanted a random value from each index but then, where did 56 come from in the second array? please be more clear about what you're trying to accomplish. – Pamblam Nov 5 '14 at 19:22
    
Adelphia what point does it have, where the "56" comes from? What i need is just taking a random value of that array? – thecakei Nov 5 '14 at 19:27

Use is_array() inside a foreach() function to check and loop inside the inner array.

foreach($array as $piece) {
    if(is_array($piece){
        foreach($piece as $item)
            $newarray[] = $item;
    } else {
        $newarray[] = $piece;
    }
}

The $newarray[] variable contains all the elements that you need to perform rand. This should solve your issue.

share|improve this answer

Try this...

$flatarray = array();
foreach($orig_array as $ra)
{
   if ( is_array($ra) )
      $flatarray = array_merge($flatarray,$ra);
   else   
      array_push($flatarray,$ra);
}
share|improve this answer

You can use a recursive function

function compress_arr($arr, &$new_arr){
    foreach($arr as $val){
        if(is_array($val)){
            compress_arr($val, $new_arr);
        } else {
            $new_arr[] = $val;
        }
    }
}
$arr_n = array();
compress_arr($array, $arr_n);

Note that this function doesn't use return, as I pass by reference the array, it will modify the variable passed.

The rest of the logic is straightforward : we iterate over the array, if it's an other array then we call the function, causing it to iterate in again. Untill it's a value, then we add the val to the new array passed by ref. This function will compress into a single-array everything that is structured as nested array, regardless of the size of the number of sublevels.

If you don't like the pass by ref, you can modify it to this:

function compress_arr($arr){
    $arr_ret = array();
    foreach($arr as $val){
        if(is_array($val)){
            array_merge($arr_ret, compress_arr($val));
        } else {
            $arr_ret[] = $val;
        }
    }
    return $arr_ret;
}

And now to take a random value in that array, just use array_rand function. It will spare you length calculating with rand function...

share|improve this answer

You could use RecursiveIteratorIterator, RecursiveArrayIterator and iterator_to_array

Your code would then look like this:

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($vals));
$newArray  = iterator_to_array($iterator, false);
var_dump($newArray);

and it gives me this result:

array(20) {
  [0]=>
  int(62)
  [1]=>
  int(9)
  [2]=>
  int(5)
  [3]=>
  int(16)
  [4]=>
  int(45)
  [5]=>
  int(11)
  [6]=>
  int(21)
  [7]=>
  int(25)
  [8]=>
  int(32)
  [9]=>
  int(50)
  [10]=>
  int(4)
  [11]=>
  int(23)
  [12]=>
  int(37)
  [13]=>
  int(57)
  [14]=>
  int(13)
  [15]=>
  int(15)
  [16]=>
  int(18)
  [17]=>
  int(22)
  [18]=>
  int(27)
  [19]=>
  int(30)
}

We can now select a random value or values from the array like this:

$items = array_rand($newArray, 10);
var_dump($items);

Which would give us these 10 keys:

array(10) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(7)
  [6]=>
  int(9)
  [7]=>
  int(13)
  [8]=>
  int(14)
  [9]=>
  int(15)
}

We can then loop through those keys and get these values:

int(9)
int(5)
int(16)
int(45)
int(11)
int(25)
int(50)
int(57)
int(13)
int(15)

So in the end you can use this code:

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($vals));
$newArray = iterator_to_array($iterator, false);
$keys     = array_rand($newArray, 10);

foreach($keys as $key){
    var_dump($newArray[$key]);
}
share|improve this answer

You can use a simple recursive iterator & simple randomized value return class:

    class Randomizer
        {
            public  static $new;

            // Recursive iterator that stores array values in the $new variable
            public  static  function Combine($array = array())  {
                    foreach($array as $key => $value) {
                            if(!is_array($value)) {
                                    self::$new[]    =   $value;
                                }
                            else
                                self::Combine($value);
                        }

                    return self::$new;      
                }

            // Returns a randomized value
            public  static  function Fetch($arr)
                {
                    // Depending on needs, you could easily
                    // add shuffle, or other array functions
                    // otherwise you can just skip this method/function
                    $val    =   array_rand($arr);
                    return $val;
                }
        }

// If you had more advanced functions in the Fetch() class use this way
$test   =   Randomizer::Fetch(Randomizer::Combine($array));

// Or simply use
$test   =   array_rand(Randomizer::Combine($array));

print_r($test);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.