简体   繁体   中英

Fill a php array with the content of other php arrays

I've been searching for a while now to accomplish my question, so I decided to ask it here.

My problem.

I've filled 2 arrays with content from a database like this:

    $query = "SELECT table_1, table_2 FROM questions";

    // Execute query or trow an error
    $results = mysqli_query($conn, $query) or die(mysql_error());

    $resulttablet1= array();
    $resulttablet2= array();

    while($row = mysqli_fetch_array($results))
    {
        // Add the right questions to the array
        $resulttablet1[] = $row['table_1'];
        $resulttablet2[] = $row['table_2'];

    }

So I've now got 2 arrays, each filled with the content of one table. This is all working fine. Now I want to put those two arrays into one array so it acts like one big array. Something like this:

$newarray = array();
$newarray[$resulttablet1,  $resulttablet2];

or

$newarray = array($resulttablet1,  
                        $resulttablet2);

Then I want to echo $newarray and show all the elements of the other two arrays.

I know I can echo both arrays separately, but this is not possible for the goal I'm trying to accomplish.

Thanks in advance.

Edit I realise my question isn't clear enough, I'll try to explain it a bit better.

On top of my question I want to show the the content of both arrays one by one on button click. That's what I'm doing at the moment like this:

// I retrieve a value from a javascript file, where I add 1 to a variable each time a button is clicked then I send this value to the server using jQuery Ajax
$value = (int)$_POST["question_nmbr"];  

// I use $value to echo the right element out of the array.
echo "<li>$resulttablet1[$value]</li>";

Everytime the button is clicked ajax loads the php file and the value is increased so the next question is loaded.

I want to do the same thing but now with an array which has multiple arrays inside of it.

array_merge doesn't do the trick I think, cause print_r($result); gives me all the content of the array.

I hope my question is a little bit more clear now.

I found the answer, thanks to someone who removed his answer already.

array_merge did the trick after all, I don't know what I did wrong the first time but now I can echo it just fine.

Here is how it works:

// I retrieve a value from a javascript file, where I add 1 to a variable each time a button is clicked then I send this value to the server using jQuery Ajax
$value = (int)$_POST["question_nmbr"];  

$newarray = array_merge($resulttablet1, $resulttablet2);

echo "<li>$newarray[$value]</li>";

Hope someone will find this useful, if not ask for more info :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM