in_array help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dink87522
Forum Newbie
Posts: 7
Joined: Mon Dec 28, 2009 10:19 pm

in_array help

Post 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
dink87522
Forum Newbie
Posts: 7
Joined: Mon Dec 28, 2009 10:19 pm

Re: in_array help

Post by dink87522 »

Solved.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: in_array help

Post 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;
} ?>
dink87522
Forum Newbie
Posts: 7
Joined: Mon Dec 28, 2009 10:19 pm

Re: in_array help

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: in_array help

Post by daedalus__ »

Code: Select all

explode(" ", $q20)
dink87522
Forum Newbie
Posts: 7
Joined: Mon Dec 28, 2009 10:19 pm

Re: in_array help

Post by dink87522 »

Why would I do that? 'D' is my break up character.

I tried it and it still prints 'Array'.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: in_array help

Post 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.
Post Reply