Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 20th, 2007, 04:56 AM
Registered User
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP array_push() for associative arrays

Hello There,

I am hoping that someone can help me. I am struggling a little bit with PHP arrays. I am relatively new to PHP programming. I am in the process of writing a web application for a college thesis. The system is an online multiple choice system. I have a web form that selects questions and answers from a MySQL database and presents them to the users via a series of multiple choice questions. The user selects a radio button as an answer for each question and submits the form back to a PHP script.

The script should then ‘check’ the answers against the database and build a page to show the answers that were correctly or incorrectly answered. I can get this to work roughly by parsing all of the form fields using a while loop and for each run through the loop comparing the answer given against the database answer and outputting a ‘correct’ or ‘incorrect’ message. On a small scale this is working, it knows the answers that are correct and incorrect etc.

The script is way to busy however and it is not likely to work in the larger scale. I was thinking that it may be better to write the question id, answer given and correct answer into a data structure like an array. This would allow me to collect the data right up to the end where I could do the checking against the database in one swoop and build the results page. The quiz could be relatively large then but all of the data is available in the array and verifying the answers could be achieved by dissecting the array.

1. If I create an array and wish to add another element to it can I use an associative array. The only examples I have seen using array_push() are very simple

    $stack = array(

    array_push($stack, "apple", "raspberry");

Can I for example use the same array_push command to add a key_pair value to an array??

    $stack = array(
        "orange" => “Vitamin C”,
        "banana" => “Potassium”

    array_push($stack[“apple”] => "Vitamin A");

I have tried a number of variances of this and cannot seem to get it to work.

2. I am using a while loop to obtain all of the question and answer values submitted on the form. I am varying a count so that I replicate the name of the form field (“question” . $count) = “question1” and “question2” etc. I am then passing this derived variable name to get the value from the $_POST superglobal. This gets me the value that was submitted but it is not permanently storing it. Once the while loop goes around again some of the variables are reassigned. I am looking for a way to permanently add the value to a data structure.

I had thought about using an array along the lines of the following

    $test_array= array (
        $question_id => array(
            "sesson_id" => $session_id,
            "answer_given" => $answer_id,
            "right_answer" => $right_answer_id,
            "quiz_q_no" => $quiz_q_count)

If however I put this within the while loop the array is created every time and the previous array is destroyed.

3. Is it possible to create the $test_array= array (); outside of the while loop and push values on inside of the while loop? Again this comes down to how do I push a key pair value onto an array?

Thanks so much for your help


Old January 9th, 2010, 01:13 PM
Registered User
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Post Answer to question #1

I found a similar issue to problem number one at devshed.com

The last post on the devshed forum topic, it says you can use array_merge to do what your looking for.

Php docs on array_merge

Here is my working script:

PHP Code:
            $first_array = array(
'name1' => 'value1',
'name2' => 'value2',
$second_array = array('name3'=>'value3');
$result array_merge((array)$first_array, (array)$second_array);
Array ( [name1] => value1 [name2] => value2 [name3] => value3 )

Last edited by zechdc; July 9th, 2010 at 02:34 PM..

Similar Threads
Thread Thread Starter Forum Replies Last Post
Associative arrays not shown at book brasilino BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 December 16th, 2006 08:51 AM
associative arrays as objects left out adspring BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 June 5th, 2006 01:37 PM
php compare 2 arrays lucian PHP How-To 0 February 23rd, 2006 12:53 PM
Mysql,PHP and arrays swedge MySQL 1 February 15th, 2006 07:55 AM
Receiving associative arrays chrscote Classic ASP Basics 0 June 14th, 2005 12:38 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.