Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6990825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:24:31+00:00 2026-05-27T19:24:31+00:00

Im making a simple poker script in PHP and up to the point where

  • 0

Im making a simple poker script in PHP and up to the point where im analysing the players hand of 5 cards.

I have the hand stored in an array ($hand) like:

Array (
    [0] => Array (
        [face] => k
        [suit] => d
    )
    [1] => Array (
        [face] => 6
        [suit] => s
    )
    [2] => Array (
        [face] => 6
        [suit] => h
    )
    [3] => Array (
        [face] => 4
        [suit] => d
    )
    [4] => Array (
        [face] => 7
        [suit] => h
    )
)

I’m not sure how to start of with finding results. For example how would I find out if the player has FOUR OF A KIND, or 4 cards with the same face?

Or if the player gets a RUN of consecutive faces (3,4,5,6,7)?

(I’m not very good at arrays)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:24:32+00:00Added an answer on May 27, 2026 at 7:24 pm

    The four-of-a-kind is simple enough. You loop over your array of cards, and add up how many of each face you have:

    $have = array();
    
    foreach($hand as $card) {
       $have[$card['face']]++;
    }
    

    This would give you

    $have = array(
        'k' => 1,
        '6' => 2,
        '4' => 1,
        '7' => 1
    );
    

    You then search this new array to see if any of the values are 4. If you have a 4, then you’ve got a 4-of-a-kind. In this case, you’ve got a single two-of-a-kind and a bunch of singles.

    For the consecutive runs, you’d need to sort the original array by suit, then by face, so you get all the diamonds together, all the hearts together, etc… and within each suit, the invididual cards are in ascending order. Then a simple “state machine” to check if you’ve got a run of 5. Assuming that your hand array is already sorted, and that the ‘face’ cards are represented by numerical values (‘j’ -> 10, ‘q’ => 11, ‘k’ => 12, ‘a’ => 13):

    $last_suit = null;
    $last_face = null;
    $consecutive = 0;
    
    foreach($hand as $card) {
       if ($last_suit != $card['suit']) { // got a new suit, reset the counters
          $consecutive = 0;
          $last_face = $card['face']; // remember the current card
          $last_suit = $card['suit']; // remember the new suit
          continue; // move on to next card
       }
       if (($card['face'] - $last_face) == 1)) {
          // the new card is 1 higher than the previous face, so it's consecutive
          $consecutive++;
          $last_face = $card['face']; // remember the new card
          continue; // move on to next card
       }
       if ($consecutive == 5) {
          break; // got a 5 card flush
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I'm making a simple grid based game, for example, I might have a
I am making simple graphical game in WinForms and currently I would like to
Making a simple three question PHP quiz. Each question is displayed on a page
I'm making a simple cart program in php. What the code below does is
making a simple movie review site to practice PHP. on one page ( a
i am making simple autosuggestion (autocompleter) plugin with jQuery. Unfortunately i have to use
I'm making simple http post request using libcurl to index.php file on my web
Making a simple program which will generate a multiple choice form. I have an
Im making a simple alternative to the default linux system monitor. Im looking to
I am making a simple game in order to learn a new language. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.