View Full Version : Values in arrays.

04-29-2006, 01:57 AM
I have an array that is processing values from a form. My array is being recieved correctly from the form.

Array ( [answer] => Array ( [0] => 1 ) [TimeOfDay] => Morning [Topics] => Yes [filename] => questionnaire1 )

I'm having some trouble with some codec while processing it. My code is like so:

$e = 1;
$b = count($array["answer"]);
for ($i=0; $i < $b; $i++)
if ($answer[$i]== $e) {
$answer_sql[$e] = $e;

echo "<br>";
echo "The if value of answer_sql is $answer_sql[$e]" . "<br>";

}else {
$answer_sql[$e] = 0;

echo "<br>";
echo "The else value of answer_sql is $answer_sql[$e]" . "<br>";

I never get a response from the first "echo". It skips to the second "echo" in the "else". I've been looking at this forever and I'm pretty sure my problem is with "$answer[$i]== $e". The purpose is to pull the value from answer of the array and compare it, but I don't think I'm doing it the right way. With the received array like so it should never get to the "else".

04-29-2006, 02:23 AM
Can you do a var_dump() for the $array and $answer arrays, and post the output?

It's not clear what is in these arrays.

04-29-2006, 04:56 AM
here is var_dump($array):

array(4) { ["answer"]=> array(1) { [0]=> string(1) "1" } ["TimeOfDay"]=> string(7) "Morning" ["Topics"]=> string(3) "Yes" ["filename"]=> string(14) "questionnaire1" }

and here is var_dump($array["answer"]):

array(1) { [0]=> string(1) "1" }

At other times there will be more values in the $answer array which I will increment through and compare with $e.

04-29-2006, 05:16 AM
sorry if I wasn;t clear...also, what is in the array $answer from this line of code:

$answer[$i]== $e

04-29-2006, 05:27 AM
Basically, it's like this...for this line of code:

$b = count($array["answer"]);

$b in this case is clearly equal to 1.

so your code is really saying this:

for ($i=0; $i < 1; $i++)
if ($answer[$i]== $e)
// do something
// do something else

you;re going through your for loop ONLY one time. For the one time the loop is executed, your condition is this:

$answer[0] == 1

It is clear that $answer[0] is NOT equal to 1, since the code inside your else statement is being executed. there inlies your problem...

04-29-2006, 06:34 AM
Sorry for being such a nub :o
I just did this simple code:

echo "The value in answer 0 is $answer[$i]" . "<br>";
echo "<br>";

And my result was:

The value in answer 0 is

I got no result back. So I have to be accessing the $answer part of $array incorrectly?
In this case $answer[0] should be equal to 1 from my recieved array.

Here is my recieved array:

Array ( [answer] => Array ( [0] => 1 ) [TimeOfDay] => Morning [Topics] => Yes [filename] => questionnaire1 )

04-29-2006, 06:42 AM
Just to clarify. The reason $answer is an array inside of $array is because it will contain more than one value like so:

Array ( [answer] => Array ( [0] => 1 [1] => 3 [2] => 16 ) [TimeOfDay] => Afternoon [Topics] => Yes [filename] => questionnaire1 )

04-29-2006, 08:58 AM
Do a var_dump($answer) and then see what you get....;)

05-01-2006, 11:29 PM
Just wanted to say thanks! Getting var_dump($answer)=NULL helped more than you can imagine. Everything working great now!
Thanks again. :D

05-02-2006, 01:46 AM
you're welcome, glad you figured it all out and it's working for you now. :thumbsup: