如何使此列表与其中显示的空白字段一起使用?

请帮我找出一个合乎逻辑的解决方案来使此类播放器列表正常工作


我有一个充满数据的数组


player_number player_name

-------------|-------

1            | player1

2            | player2

3            | player3

6            | player6

8            | player8

12           | player12

-------------|-------

因此,如您所见,可能会遗漏一些数字,但最大值player_number始终为 50,所以问题是我如何显示所有 50 个字段,尽管也必须显示丢失的数字但在其字段上填充“-”


我的逻辑问题是 my_array[$i] 像 1 到 6 一样(上面显示了列表)所以我的 for() 循环不能像那样运行它my_array[$i] == player_number


我想在页面上显示它


1            | player1

2            | player2

3            | player3

4            | -

5            | -

6            | player6

7            | -

8            | player8

9            | -

10           | -

11           | -

12           | player12

-------------|-------

编辑:


当我使用提供的代码时它可以,但是当我尝试用我的代码实现它时,逻辑失败


    HowIGrabMyData() {

        $query = $this->db->query($sql); // "SELECT * from `players` etc...


        $output = array();


        foreach ( $query->rows as $result ) {

            $output[] = array(

                'player_number' => $result['player_number'],

                'player_name' => $result['player_nick']

            );


        }


        return $output;

    }


    $players = HowIGrabMyData();


    <?php for($i=1; $i <= 50; $i++){ // loop for 50 times

        $player_name = "-";


        if(isset($players[$i]['player_name'])){ // if the player name is set, use it

            $player_name = $players[$i]['player_name'];

        }

        echo $i. " | ".$player_name."<br>";

    }

它仍然在一行中显示 1 到 6 行,而不是我想要的方式,在 50 个字段中都是空白的我仍然想念主要逻辑......


汪汪一只猫
浏览 121回答 1
1回答

胡子哥哥

新答案。我已经用你的新数组格式更新了我的答案。这次我们创建了一个包含 50 个索引的数组,所有索引都设置为“-”。然后我们检查输出数组并将玩家姓名放在具有 50 个索引的数组中的正确位置。最后,我们检查 50 个索引并将它们打印到屏幕上。这是代码:&nbsp; &nbsp;<?php$output= array(&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; "player_number" => 1,&nbsp; &nbsp; &nbsp; &nbsp; "player_name"=> "player1"&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; "player_number" => 2,&nbsp; &nbsp; &nbsp; &nbsp; "player_name"=> "player2"&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; "player_number" => 3,&nbsp; &nbsp; &nbsp; &nbsp; "player_name"=> "player3"&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; "player_number" => 6,&nbsp; &nbsp; &nbsp; &nbsp; "player_name"=> "player6"&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; "player_number" => 8,&nbsp; &nbsp; &nbsp; &nbsp; "player_name"=> "player8"&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; "player_number" => 12,&nbsp; &nbsp; &nbsp; &nbsp; "player_name"=> "player12"&nbsp; &nbsp; ),);$player_array = array();// Create an array with 50 indexes all set to '-'for($i=1; $i <= 50; $i++){ // loop for 50 times&nbsp; &nbsp; $player_array[$i]= "-";}// update the player array with names;foreach($output as $value){&nbsp; &nbsp; $player_array[$value['player_number']] = $value['player_name'];}// print out numbers and&nbsp; namesforeach($player_array as $number => $name){&nbsp; &nbsp; echo $number. " | ".$name."<br>";}旧答案您必须创建一个运行 50 次的 for 循环,检查数组中的索引是否有值,以及该索引处是否有使用该名称。否则它只会使用 - 作为玩家名称。尝试这个:<?php&nbsp; &nbsp;$my_array= array(&nbsp; &nbsp; 1 => "player1",&nbsp; &nbsp; 2 => "player2",&nbsp; &nbsp; 3 => "player3",&nbsp; &nbsp; 6 => "player6",&nbsp; &nbsp; 8 => "player8",&nbsp; &nbsp; 12 => "player12",);&nbsp;&nbsp;for($i=1; $i <= 50; $i++){ // loop for 50 times&nbsp; &nbsp; &nbsp; &nbsp; $player_name = "-";&nbsp; &nbsp; &nbsp; &nbsp; if(isset($my_array[$i])){ // if the player name is set, use it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $player_name = $my_array[$i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo $i. " | ".$player_name."<br>";&nbsp; &nbsp; }?>查看isset()函数。你也可以使用array_key_exists()代替 isset()。像这样:for($i=1; $i <= 50; $i++){&nbsp; &nbsp; $player_nick = "-";&nbsp; &nbsp; if(array_key_exists($i,$my_array)){ //if value of $i is an index in the array&nbsp; &nbsp; &nbsp; &nbsp; $player_nick = $my_array[$i];&nbsp; &nbsp; }&nbsp; &nbsp; echo $i. " | ".$player_name."<br>";}另请查看isset() 与 array_key_exists()。方便知道两者之间的区别。
打开App,查看更多内容
随时随地看视频慕课网APP