Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9

Thread: Array Question

  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    What is the way using php to specify all the values in an array?

    for example if i had an array called fruit...
    PHP Code:
    $fruit = array("apple""orange""pear"
    then an if statment to check for fruit....
    PHP Code:
    if ($selection $fruit[?]) {
    $choice 'you have picked a fruit';
    } else {
    $choice 'you have not picked a fruit';

    what would go in place of ? to check all of the values in the array?

    thanks
    JR

  2.  

  3. #2
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Here's a link to array functions:http://us2.php.net/Array

    However, waht you want specificially:
    in_array

    (PHP 4 )
    in_array -- Return TRUE if a value exists in an array
    Description
    bool in_array ( mixed needle, array haystack [, bool strict])
    you woudl have:
    PHP Code:
    $choice 'you have not picked a fruit';
    foreach(
    $fruit as $value){
    if (
    in_array($value$selection$choice 'you have picked a fruit';

    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  4. #3
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    thanks
    JR

  5. #4
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    also, how did you get on here brak?, as we are the only 2 people on including guests
    JR

  6. #5
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    I don't know... even more fishy, this computer works while my laptop does not... Yet they are behind the same router in my room (only two on it currently) so I don't see how it oculd be different :P
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  7. #6
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    Originally posted by Brak
    PHP Code:
    $choice 'you have not picked a fruit';
    foreach(
    $fruit as $value){
    if (
    in_array($value$selection$choice 'you have picked a fruit';

    The foreach is unnecessary...
    PHP Code:
    $choice 'you have not picked a fruit';
    if (
    in_array($selection,$fruit)
      
    $choice 'you have picked a fruit'
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)

  8. #7
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    i know, i modified the code that brak so kindly supplied and but it in a few if statements etc.
    JR

  9. #8
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Heh... well here it is in one line :-P
    PHP Code:
    $choice in_array($selection,$fruit) ? 'you have picked a fruit' 'you have not picked a fruit'

  10. #9
    Senior Member james's Avatar
    Join Date
    May 2003
    Location
    Melbourne, AUSTRALIA
    Posts
    364
    Member #
    1352
    I'm trying to think of a way to one-up that... ;-)
    James H
    Home Page · Mars Page · www.fihsf1.net (formerly www·fihs·net)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:50 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com