Question

I want to be able to pull a single record from the database, store that record into an array, and then show a random value from that array, and that random value cannot be the 0 index position because that is the record's ID field.

I have two tables. One is questions and the other is answers. Questions has two fields, ID and Question. Answers has 10 fields: ID, ANS1, ANS2, ANS3, etc. So, Answers is associated with Questions by their ID.

To get the record, I am using this:

$json = array( );
mysql_select_db($database_localhost, $localhost);
$query_getSentence = "SELECT * FROM answers WHERE ID = 13";
$getSentence = mysql_query($query_getSentence, $localhost) or die(mysql_error());
while ( $row_getSentence = mysql_fetch_assoc($getSentence)) {
$json[] = $row_getSentence;
$totalRows_getSentence = mysql_num_rows($getSentence);

So, when I use print_r($json), I get this:

Array (
    [0] => Array (
        [ID] => 13
        [ANS1] => ducks
        [ANS2] => chickens
        [ANS3] => birds
        [ANS4] => cats
        [ANS5] => dogs
        [ANS6] => elephants
        [ANS7] => monkeys
        [ANS8] => animals 
        [ANS9] => bees
    )
)

Now, if I understand this correctly, I think you get a random result from an array using this:

echo $myArray[array_rand($myArray)];

But it doesn’t work if I put $json where I just wrote $myArray. I know I have to use json_encode, but no matter how many different ways I try to insert that in the above code, it doesn’t do anything still.

Also, to skip the first index (the ID field), the way I've tried it is doing this:

echo(array_slice($myArray,1,9));

That returns all the Answers and leaves out the ID. But, once again, I can’t figure out how to combine all this stuff into a working piece of code. How, do I use all of this together, so it works correctly?

Was it helpful?

Solution

How about just using shuffle and grabbing the first item off of the array like this:

shuffle($myArray)
echo $myArray[0];

But honestly, your code is confusing. So what is the output of the $json array—that isn’t JSON—if you do this?

echo '<pre>';
print_r($json);
echo '</pre>';

EDIT: With the updated question from the original poster, it seems like the array is nested in another array based on the structure:

Array (
    [0] => Array (
        [ID] => 13
        [ANS1] => ducks
        [ANS2] => chickens
        [ANS3] => birds
        [ANS4] => cats
        [ANS5] => dogs
        [ANS6] => elephants
        [ANS7] => monkeys
        [ANS8] => animals 
        [ANS9] => bees
    )
)

Okay, so knowing that you would do this. I assuming that $json is the array in this case:

$new_array = array_slice($json[0],1,9);
$random_key = array_rand($new_array);
$final_value = $new_array[$key];

So array_slice is grabbing items 1-9 from $json[0] and assigning it to $new_array. Then we use array_rand() to get a random key value from $new_array. Now the final value is set as $final_value with $new_array[$key].

I am using array_rand here instead of the shuffle I used before because it’s clear you want to preserve keys.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top