Page 1 of 1

in_array help

Posted: Mon Dec 28, 2009 10:23 pm
by dink87522
My code on one page is:

Code: Select all

$_SESSION['q20'] = $countries[$rndWork]."D".$answer."D".$answer_result;
and on the next page is:

Code: Select all

$q20 = $_SESSION['q20'];
for ($i = 0; $i < 3; $i = $i + 1){
    $q20Split = array_slice(explode("D", $q20), i, 1);
    $question20[i] = $q20Split; 
} ?>
however I get the following error which I have never seen before:

Warning: array_slice() expects parameter 2 to be long, string given in /home/eddiegou/public_html/geographytrainer.com/hsDis.php on line 31

Warning: array_slice() expects parameter 2 to be long, string given in /home/eddiegou/public_html/geographytrainer.com/hsDis.php on line 31

Warning: array_slice() expects parameter 2 to be long, string given in /home/eddiegou/public_html/geographytrainer.com/hsDis.php on line 31

Re: in_array help

Posted: Mon Dec 28, 2009 10:33 pm
by dink87522
Solved.

Re: in_array help

Posted: Mon Dec 28, 2009 11:05 pm
by requinix
To those curious, there was a $ missing on lines 3 and 4.

Code: Select all

$q20 = $_SESSION['q20'];
for ($i = 0; $i < 3; $i = $i + 1){
    $q20Split = array_slice(explode("D", $q20), $i, 1);
    $question20[$i] = $q20Split;
} ?>

Re: in_array help

Posted: Tue Dec 29, 2009 12:52 am
by dink87522
Hmm, still doesn't work.

q2a prints "GrenadaDSt. George'sD0"
while q0 prints "Array", q1 prints "Array" and q2 prints "Array"

I want it so that q0 prints "Grenada", q1 prints "St. George" and q2 prints "0".

Sorry I am only a beginner at PHP.

Re: in_array help

Posted: Tue Dec 29, 2009 12:55 am
by daedalus__

Code: Select all

explode(" ", $q20)

Re: in_array help

Posted: Tue Dec 29, 2009 1:01 am
by dink87522
Why would I do that? 'D' is my break up character.

I tried it and it still prints 'Array'.

Re: in_array help

Posted: Tue Dec 29, 2009 2:15 am
by requinix
You can't print arrays. You need to do something else first, such as translate them into strings or, for less aesthetically-pleasing results, try print_r or var_dump.