array_rand problem

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

array_rand problem

Post by someguyhere »

I'm trying to load a random series of rows from a multidimensional array but I am getting an error saying Illegal offset type. What am I doing wrong here?

Code: Select all

		$paragraph = $new_page[2][array_rand($new_page[2], $random)];
Here's an example of how my array is set up:

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: array_rand problem

Post by Jonah Bron »

Please don't double-post.

viewtopic.php?f=1&t=125438
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: array_rand problem

Post by someguyhere »

It's not a double post. If you had read the post you would see that it's a completely different situation.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: array_rand problem

Post by califdon »

someguyhere wrote:It's not a double post. If you had read the post you would see that it's a completely different situation.
Perhaps it's not a double-post, but the same answer applies: read the description carefully of what array_rand() does and what its arguments are. Where does the value for $random come from? Are you trying to extract just one array element or multiple elements as the value of $paragraph? If you expect a simple value for $paragraph, the second argument to array_rand() should be "1" or just omit it (it's optional). If you really expect it to be an array, where are you getting the appropriate number of elements from, as $random?

Please, we are not trying to hassle you. We want to help you. But cooperation on your part will result in more happiness all around and more useful help for you.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: array_rand problem

Post by Jonah Bron »

someguyhere wrote:It's not a double post. If you had read the post you would see that it's a completely different situation.
Oh yeah, I just glanced at it and it looked exactly the same. :) califdon is right though, you should take a closer look at what array_rand does. You can't use an array as an index.

What is $random? I'm guessing it's a random number :roll:, but what is it's range?
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: array_rand problem

Post by someguyhere »

I did read it, but I seem to be missing something.

Re: the $random variable, it's declared a bit earlier in the script. I've tested it using var_dump and echo and that does kick out a number in the range specified. I also tried replacing the $random variable with just a number, but still got the same error (Illegal offset type).

Re: $paragraph, the intent is for it to be populated with a random number of values from the original array (the contents of $new_page[2]).

Does that clarify my goal and problem?

By the way, if I came off as short or rude in my last post, that wasn't my intent :)
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: array_rand problem

Post by someguyhere »

What is $random? I'm guessing it's a random number :roll:, but what is it's range?

Code: Select all

		$random = rand(6, 12);
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: array_rand problem

Post by someguyhere »

Ok, I don't know if this is the optimal approach, but it works:

Code: Select all

		$random = rand(6, 12);
		$paragraph = $new_page[2];
		$paragraph = array_rand(array_flip($paragraph), $random);
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: array_rand problem

Post by someguyhere »

I've made it one step further, but I'm pretty sure that there's a way to include the shuffle into the same line as array_rand. Everywhere I've tried to insert it seems to interfere with the other the other functions. Any ideas?

Code: Select all

		$random = rand(6, 12);
		shuffle($new_page[2]);
		$paragraph = implode(array_rand(array_flip($new_page[2]), $random));
Post Reply