0

I'm trying to display blocks of text from a list of blocks.

I'm thinking that an array makes the most sense...

$quotes[] = array(
    'block'  => 'Luck is what happens when preparation meets Opportunity.',
    'author' => 'Seneca',
);
$quotes[] = array(
    'block'  => 'Quote number two.',
    'author' => 'Author Two',
);
$quotes[] = array(
    'block'  => 'Quote number three.',
    'author' => 'Author Three',
);

Now if I wanted to list the quotes I would do this:

foreach($quotes[] as $quote) {
    echo '<div><p>"<i>' . $quote['block'] . '</i>"<br />― ' . $quote['author'] . '</p></div>';
}

But how do I go about listing just one of the quotes randomely?

I was looking around and some people were using while loops?

The end goal is that wherever I place this piece of code, I want to display a random quote in different parts of my website, so I don't want the same quote to be in every spot.

Matthew
  • 673
  • 1
  • 7
  • 16
  • 1
    `shuffle($quotes)`; then just `$displayQuote = array_pop($quotes);` and display that one.... [shuffle()](http://www.php.net/manual/en/function.shuffle.php), [array_pop()](http://www.php.net/manual/en/function.array-pop.php) – Mark Baker May 19 '15 at 21:39
  • How do I display the first one in the shuffled list? I didn't think it would be that simple lol... Every question I read had a mess of math. – Matthew May 19 '15 at 21:41
  • 1
    `echo $quotes[array_rand($quotes)];` –  May 19 '15 at 21:41
  • Do you want to put that into a question so I can answer it? -- EDIT: into an answer to I can accept it***** – Matthew May 19 '15 at 21:41
  • That's what I meant. – Matthew May 19 '15 at 21:44
  • Yeah it is a duplicate, not sure how I didn't see that original question before. – Matthew May 19 '15 at 21:46
  • @Dagon I like your method, it's the same as the apparent duplicate answer, but I can't display the quote and the author this way. How do we make this work? – Matthew May 19 '15 at 21:53
  • 1
    you have to store the random key picked then use it, like so: `$rand_key=array_rand($quotes); echo $quotes[$rand_key]['block']; echo $quotes[$rand_key]['author'];` Alternative: `$picked =$quotes[array_rand($quotes)]; echo $picked['block']; $picked['author']` –  May 19 '15 at 21:55
  • @Dagon thanks, make an answer so I can accept it please =) – Matthew May 19 '15 at 21:57
  • question is closed, it can't be answered –  May 19 '15 at 21:58
  • @Barmar I believe this question is different than the other one because this also needs to solve the problem of using a multi dimensional array - which the other one didn't show me – Matthew May 19 '15 at 21:58
  • 1
    Once you select an element, you have an array that you can use just like any other array. – Barmar May 19 '15 at 21:59
  • @Dagon hey I'm trying to get this working but it's not budging for me, I'm using a php sandbox here http://sandbox.onlinephpfunctions.com/code/b4bd4b0f012a2bd6d1bb970ec14ad92837ebbd1b – Matthew May 19 '15 at 22:14
  • It's no different, you're still only selecting an element from the top-level of your $quotes array, it's only the displaying of that element that's slightly different (unless you want to pick a random block and a random author independently) – Mark Baker May 19 '15 at 22:15
  • @MarkBaker I understand the question is very similar, not disputing that, just saying that the result is not the same thing. – Matthew May 19 '15 at 22:21
  • 1
    your demo is missing the `[]` you use above –  May 19 '15 at 22:22

1 Answers1

1

I believe it's as simple as just getting random integer from 0 to count($quotes).

That could be done like in this question.

In your case:

echo $quotes[rand(0, count($quotes) - 1)]['block'];
Community
  • 1
  • 1
Marek Lisý
  • 3,302
  • 2
  • 20
  • 28