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(
    "orange", 
    "banana"

);
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.

  1. 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.

  1. 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

Regards,

Claire

I think both of your questions can be answered pretty easily:

An array is nothing but a pile of variables. Nothing special about it.

What array_push() exactly does, I dunno, but I’ve never had to use it, and I’ve been using arrays since the day I was born (well, kinda). What you need to realize, is what exactly your code does:

[php]$stack = array(
“orange”,
“banana”
);[/php]

This code creates an array, containing ‘orange’ and ‘banana’ as its data, and assigns this array to the variable $stack.

Basically the outcome is this: $stack[0] == “orange” and $stack[1] == “banana”.

If you want to add anything to it, it’s possible like this:

[php]$stack[2] = “peach”;[/php]

Or, if you want it to be associative:

[php]$stack[‘peach’] = “peach”;[/php]

That way, you don’t overwrite the existing data in the $stack variable (unless you use an already existing index), and create a new index with a new value.

If you use the construction $stack = {SomeValueHere}; then it will reassign the value $stack to hold a new value altogether, losing any values it already held. SomeValueHere could be an integer, string, object, resource or array, it really doesn’t matter.

I have used array_push before and here is brief example/description:

Array Push simply appends a value to a specified array

[php]<?

$my_string = “Me”;
$my_array = array();

array_push($my_array, $my_string);

for($i=0; $i < count($my_array); $i++)
{
echo $my_array[$i];
}

//OUTPUTS: Me
?>[/php]
Simply the same as doing $my_array[] = $string;
or $my_array[0] = $string;

I beg to differ. This would be overwriting the value of the first array index with the value in $string. It would only add the index 0 if it didn’t already exist.

Yes, you are correct it would over write if there was a value there, I was running under the empty array assumtion and you would need to use a loop to populate that way as well. $my_array[$i] = $string;

So yes that was pretty much incorrect saying $my_array[0] = $string; is the same as the others, but with some added functionality it would become similar to the others.

The loop code snippet that could be used for populating an array with a numerical index could look something like this:

[php]
for ($i = 0; $i < $someValue; $i++) {
$myArray[$i] = $string;
}
[/php]

Yep, exactly what I was thinking…

Only that seems to be pretty counter productive unless you had a very specific need for it when you could just use one of the previously stated methods of doing it.

Naturally, a simple loop like that is usually only good for defaulting all the (numerical) indexes of an array to a certain value. It was more of an example of how to do it than anything else, heh :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service