doufutao4428 2015-04-29 18:30
浏览 25
已采纳

php阵列整理出胜利者

I have an variable called $result that is an array containing upto 4 elements. one of the elements can be an number, or an array containing multiple numbers.

The first element in $result are the winner, the 2nd element is 2nd etc.. It can be multiple winners, 2nd winners, 3rd etc..

example:

    $result = array(
1,
2,
array(3,4)
);

OR

    $result = array(1,2,3);
OR
    $result = array(array(1,2),array(1,2));
    OR

    $result = array(1,2,3,4);
etc..

this would mean that the winner is 1, the 2nd winner is 2, and theres a split 3rd place between 3 and 4. no 4th place as $result only contains 3 elements.

Question: How can store the winners to be like this:

   $data['winner'] = "The user Xwon"; OR "the user x and y won.. (or even more if there is even more winners somehow)";
    // If there is 2nd place
    do the same with 2nd.
    // If there is 3rd..
    same
    // If there is 4th.
same

so $data can contain an element for each of the winners, no matter if there multiple winners for that occurence or not?

If you have a better solution please share.

Brief question: How can i make the output of this variable better?/ best possible way?

Note: its important that all the first winner will be announced in a message.

$data['winner'] = "The user Xwon"; OR "the user x and y won.. (or even more if there is even more winners somehow)";

this is what i had from before, very poor made:

   $winner = $result[0];

        $differentWinners = count($result);


        if (is_array($winner)) {
            foreach($winner as $player) {
                echo "winner is: $player <br>";
            }
        } else {
      //      echo "only winner is: $winner <br>";
        }

       $return['winner']['userid'] = $userIds[$winner];
        $return['winner']['username'] = $this->user->userDetails($userIds[$winner])->username;
        $winnerData  = new pokerHand($test[$winner]);
        $return['winner']['combination'] = $winnerData->printHand()['combination'];

        $return['winner']['message'] =  "THE AMAZING  ".$return['winner']['username']." WON WITH ".$return['winner']['combination']." !";




        if ($differentWinners == 2) {

            $secound = $result[1];
            if (is_array($secound)) {
                foreach($secound as $player) {
                    echo "secound is: $player <br>";
                }
            } else {
                echo "only secound is: $secound <br>";
            }
        }


        if ($differentWinners == 3) {
            $third = $result[2];
            if (is_array($third)) {
                foreach($third as $player) {
                    echo "third is: $player <br>";
                }
            } else {
                echo "only third is: $third <br>";
            }
        }




        if ($numbers == 4 and $differentWinners == 4) {
            $forth = $result[3];
            if (is_array($forth)) {
                foreach($forth as $player) {
                    echo "forth is: $player <br>";
                }
            } else {
                echo "only forth is: $winner <br>";
            }
        }



   return $return;
  • 写回答

2条回答 默认 最新

  • dongxiong4571 2015-04-29 18:35
    关注

    This should work for you:

    Here I simply go through the $result array an check if it is either an array or a normal number. Then I add the values to the $data array.

    <?php
    
        $result = [1,[2,3],4];
        $names = ["first", "second", "third", "fourth"];
    
    
        foreach($result as $k => $v){
            if(is_array($v)) {
                foreach($v as $t)
                    $data["winner"][$names[$k]][] = $t;
            } else {
                $data["winner"][$names[$k]][] = $v;
            }
        }
    
        print_r($data);
    
    ?>
    

    output:

    Array
    (
        [winner] => Array
            (
                [first] => Array
                    (
                        [0] => 1
                    )
    
                [second] => Array
                    (
                        [0] => 2
                        [1] => 3
                    )
    
                [third] => Array
                    (
                        [0] => 4
                    )
    
            )
    
    )
    

    And if you want to print winner messages just define the message like this:

    $messages = ["first" => "First is user: ", "second" => "Second is user: ", "third" => "Third is user: ", "fourth" => "Fourth is user: "];
    

    And then loop through the $data array like this:

    foreach($data["winner"] as $k => $v) {
        if(is_array($v))
            echo $messages[$k] . implode(" and ", $v) . "<br>";
        else
            echo $messages[$k] . $v . "<br>";
    }
    

    output:

    First is user: 1
    Second is user: 2 and 3
    Third is user: 4
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀