Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you store random parts of an array within an array and then print as a list?
    primarykey
    data
    text
    <p><strong>Follow-up question: <a href="https://stackoverflow.com/questions/18281343/how-to-randomly-take-examples-from-arrays-shuffle-them-and-place-them-into-a-ne">How to randomly take some examples from arrays, shuffle them and place them into a new array to be formatted as a list</a></strong></p> <p>How do you store random parts of an array within an array and then print as a list?</p> <p>I know that there are answers to both parts of my question already, but the two answers I found aren't compatible with each other. I'm trying to do this with PHP, but i'm willing to use another code language if absolutely necessary.</p> <p>I made up a simple version of what I've tried to do so far. I would like to store one random question from each array ($mars, $jupiter and $earth) as another array ($all) and then reformat this array as a list to be displayed as html (using &lt; ul > and &lt; li >).</p> <p>For some reason, the first function wants the =>array(), but the second doesn't.</p> <p>This makes a formatted html-style list from an array (replace $all with the array you want to format)</p> <pre><code>function makeList($all) { //Base case: an empty array produces no list if (empty($all)) return ''; //Recursive Step: make a list with child lists $output = '&lt;ul&gt;'; foreach ($all as $key =&gt; $subArray) { $output .= '&lt;li&gt;' . $key . makeList($subArray) . '&lt;/li&gt;'; } $output .= '&lt;/ul&gt;'; return $output; } </code></pre> <p>Here I'm just making the arrays. The =>array() is needed for the formatting above. I don't know why.</p> <pre><code>$mars = array ('How big is Mars?'=&gt;array(), 'How many moons does Mars have?'=&gt;array(), 'How far away is Mars?'=&gt;array(), 'What is the highest point on Mars?'=&gt;array()); $jupiter = array ('How big is Jupiter?'=&gt;array(), 'How many moons does Jupiter have?'=&gt;array(), 'How far away is Jupiter?'=&gt;array(), 'What is the highest point on Jupiter?'=&gt;array()); $earth = array ('How big is Earth?'=&gt;array(), 'How many moons does Earth have?'=&gt;array(), 'How far away is Earth?'=&gt;array(), 'What is the highest point on Earth?'=&gt;array()); //An array of the three arrays $all = array ($mars, $jupiter, $earth); </code></pre> <p>This prints the formatted version of $all</p> <pre><code>echo makeList ($all); </code></pre> <p>This is randomly taking two questions from the array $marz and printing them</p> <pre><code>$marz = array ('How big is Mars?', 'How many moons does Mars have?', 'How far away is Mars?', 'What is the highest point on Mars?'); $rand_keys = array_rand($marz, 2); echo $marz[$rand_keys[0]] . "\n"; echo $marz[$rand_keys[1]] . "\n"; </code></pre> <p>Here I am storing these results in a new array and printing it as proof in ugly array form</p> <pre><code>$new = array ($marz[$rand_keys[0]], $marz[$rand_keys[1]]); print_r ($new); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload