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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探