Page 1 of 1

data from multidimensional array

Posted: Thu Dec 02, 2010 1:03 pm
by someguyhere
I'm trying to pull a random value from an array using the code below but it keeps returning 0. Any idea what I'm doing wrong?

Code: Select all

		$new_page_title = array_rand($new_page[2]);
My array is set up like this:

Code: Select all

Array
(
    [0] => name
    [1] => Array
        (
            [0] => data 1
            [1] => data 2
            [2] => data 3
        )

    [2] => Array
        (
            [0] => other data 1
            [1] => other data 2
            [2] => other data 3
        )

    [3] => 
    [4] => 
)

Re: data from multidimensional array

Posted: Thu Dec 02, 2010 1:08 pm
by Jonah Bron

Code: Select all

$new_page_title = $new_page[2][array_rand($new_page[2])];
If you look more closely at the man page, you'll see that it returns the key, not the value.

http://php.net/array-rand