Page 1 of 1

array_rand problem

Posted: Thu Dec 02, 2010 8:28 pm
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] =>
)
 

Re: array_rand problem

Posted: Thu Dec 02, 2010 8:41 pm
by Jonah Bron
Please don't double-post.

viewtopic.php?f=1&t=125438

Re: array_rand problem

Posted: Thu Dec 02, 2010 8:49 pm
by someguyhere
It's not a double post. If you had read the post you would see that it's a completely different situation.

Re: array_rand problem

Posted: Thu Dec 02, 2010 9:37 pm
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.

Re: array_rand problem

Posted: Thu Dec 02, 2010 9:43 pm
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?

Re: array_rand problem

Posted: Thu Dec 02, 2010 9:50 pm
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 :)

Re: array_rand problem

Posted: Thu Dec 02, 2010 9:52 pm
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);

Re: array_rand problem

Posted: Fri Dec 03, 2010 9:32 am
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);

Re: array_rand problem

Posted: Fri Dec 03, 2010 9:45 am
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));