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;

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

Here's a quick function that can return the result you asked for - the idea would be to check if the array value is an array and if it is pop the last record off and implode the rest with a comma delimiter, adding the last one back on with "and" as a delimiter (to produce 1, 2, 3 and 4 output). If it's not an array, just add it as is, then return the string you asked for:

function outputResults($test) {
    if (is_array($test)) {
        // Take the last one off so we can use AND as a delimiter
        $last = array_pop($test);
        $results = implode(', ', $test) . ' and ' . $last;
    } else {
        $results = $test;
    }
    return 'The user ' . $results . ' won.';
}

Demo here