EMail Tester

Author Topic: PHP array_push() for associative arrays  (Read 4805 times)

mcmahonc

  • New Member
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
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.

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

Regards,

Claire

PHP Help Forum

PHP array_push() for associative arrays
« on: April 21, 2007, 03:35:57 pm »


Zyppora

  • Global Moderator
  • Senior Member
  • *****
  • Posts: 1401
  • Karma: 0
    • View Profile
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:

1
2
3
4
 $stack = array(
   
"orange",
   
"banana"
 
);



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:

1
 $stack[2] = "peach";


Or, if you want it to be associative:

1
 $stack['peach'] = "peach";


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.
HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS. PHP forum for beginners.

PHP Help Forum

(No subject)
« Reply #1 on: April 21, 2007, 05:08:25 pm »


Ragster00

  • Senior Member
  • ****
  • Posts: 499
  • Karma: 0
    • View Profile
    • Help with PHP?
I have used array_push before and here is brief example/description:

Array Push simply appends a value to a specified array  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <?
  
  $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
 
?>


Simply the same as doing $my_array[] = $string;
or $my_array[0] = $string;
-- Simply the best Mail Tester! :)
Not all code provided is meant to be a working, error free code. It is meant only as a guide.

PHP Help Forum

(No subject)
« Reply #2 on: April 23, 2007, 12:32:49 pm »



PHP Help Forum

(No subject)
« Reply #3 on: April 23, 2007, 03:13:55 pm »



Zyppora

  • Global Moderator
  • Senior Member
  • *****
  • Posts: 1401
  • Karma: 0
    • View Profile
HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS. PHP forum for beginners.



 


Blacklist Checker