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

mcmahonc

  • Just Joined
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
PHP array_push() for associative arrays
« on: April 21, 2007, 03:35:57 pm »
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
(No subject)
« Reply #1 on: April 21, 2007, 05:08:25 pm »
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.

Ragster00

  • Senior Member
  • ****
  • Posts: 499
  • Karma: 0
    • View Profile
    • Help with PHP?
(No subject)
« Reply #2 on: April 23, 2007, 12:32:49 pm »
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;
-- I do what I can to help! :)
Not all code provided is meant to be a working, error free code. It is meant only as a guide.

Zyppora

  • Global Moderator
  • Senior Member
  • *****
  • Posts: 1401
  • Karma: 0
    • View Profile
(No subject)
« Reply #3 on: April 23, 2007, 03:13:55 pm »
Quote from: "Ragster00"
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.
HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS. PHP forum for beginners.

PHP Help Forum

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


Ragster00

  • Senior Member
  • ****
  • Posts: 499
  • Karma: 0
    • View Profile
    • Help with PHP?
(No subject)
« Reply #4 on: April 24, 2007, 11:35:45 am »
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.
-- I do what I can to help! :)
Not all code provided is meant to be a working, error free code. It is meant only as a guide.

Zyppora

  • Global Moderator
  • Senior Member
  • *****
  • Posts: 1401
  • Karma: 0
    • View Profile
(No subject)
« Reply #5 on: April 24, 2007, 01:52:27 pm »
The loop code snippet that could be used for populating an array with a numerical index could look something like this:

1
2
3
4
5
 
 
for ($i 0$i $someValue$i++) {
   
$myArray[$i] = $string;
 }
 

HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS. PHP forum for beginners.

Ragster00

  • Senior Member
  • ****
  • Posts: 499
  • Karma: 0
    • View Profile
    • Help with PHP?
(No subject)
« Reply #6 on: April 24, 2007, 04:14:41 pm »
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.
-- I do what I can to help! :)
Not all code provided is meant to be a working, error free code. It is meant only as a guide.

Zyppora

  • Global Moderator
  • Senior Member
  • *****
  • Posts: 1401
  • Karma: 0
    • View Profile
(No subject)
« Reply #7 on: April 25, 2007, 03:35:29 am »
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 :)
HAVE YOU TRIED DEBUGGING? Example code in this reply deliberately contains BUGS. PHP forum for beginners.