data from multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

data from multidimensional array

Post 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] => 
)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: data from multidimensional array

Post 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
Post Reply