使用in_array查找名称然后只回显找到的值?

时间:2012-10-29 08:26:26

标签: php

我确定我的头衔并不是那么有用。我要做的就是这个,我有一个带有名字的数组。我还有一个动态数组,它是使用外部xml文件中的foreach生成的。我想要做的是如果找到一个名字然后回应每个名称。我仍在努力学习我的PHP技能,但我不知道该怎样做才能让我按照我想要的方式工作。我已经搜索了几个小时,试图找到一些可以工作但没什么的东西。我可能没找对。

以下是我正在尝试的代码。

// This just gets how many players ore on the US team
$serverplayer = $gameME_sdk_object->client_api_serverinfo("".$ip."/players/usarmy");
        foreach($serverplayer['serverinfo'][0]['teams'] as $mainserver) {
            if($mainserver['name'] == "usarmy") {
                $dacount = $mainserver['count'];
            }
        }
        if($dacount >= 1)
            // Begins Admin List/Count
            $admins = array("INFIDEL_HARE", "Romania191", "mardog104", "INFIDEL_JIHAD", "stvnsng", "HellKnightFire", "McThump", "INFIDEL_JByrns93", "INFIDEL_ALPHA", "s0ck37", "Zepppster", "xRingmasteressx", "FreeeKillz", "smokert", "Ccls2", "INFIDEL_GRIF", "cuzco2585", "Prophet731", "KittensDrunk", "Rexperience-EF", "Whickerbasket", "InfamousHoole", "Cruz_5326", "-Grimreaperx9-", "JoeyT2");
            foreach($serverplayer['serverinfo'][0]['players'] as $admname) {
                $Alist[] = $admname['name'];
                if(in_array($Alist, $admins)) {
                    $adminlist = $admname['name'];
                    $countAlist = count($adminlist);
                }
            }
            // Ends Admin List/Count
            // Everything below is just the data
            echo '<div class="four-columns six-columns-tablet twelve-columns-mobile">';
            echo '<pre>';
            echo $adminlist;
            echo '</pre>';
            echo "<div class=\"table-header\">US (".$dacount.")</div>";
            echo '<table class="table responsive-table" id="scoreboard">';
            echo '<thead>';
            echo '<tr>';
            echo '<th scope="col" width="2%">Sq</th>
            <th scope="col">Name</th>
            <th scope="col" width="2%">K</th>
            <th scope="col" width="2%">D</th>
            <th scope="col" width="5%" class="hide-on-mobile hide-on-tablet hide-on-mobile-portrait">Cheat Meter</th>';
            echo '</tr></thead>';
            echo '<tbody>';
            foreach($serverplayer['serverinfo'][0]['players'] as $player) {
                echo '<tr>';
                if ($player['team'] == "unassigned" Xor $player['team'] == "None") {            
                    $team = "<small class=\"tag green-gradient glossy\">Joining</small>";
                }
                echo "<td class=\"low-padding\">".ucfirst($player['squad'])."</td>";
                echo "<td class=\"low-padding\"><a href=\"http://battlelog.battlefield.com/bf3/user/".$player['name']."\" target=\"_blank\"><img src=\"./img/battlelog.png\" alt=\"Battlelog Profile\" width=\"16\" height=\"16\"></a>&nbsp;".$player['name']."<span style=\"float: right\">".$team."</span></th>";
                echo '<td>'.$player['kills'].'</td>';
                echo '<td>'.$player['deaths'].'</td>';
                echo "<td><a href=\"http://panel.dev.adkgamers.com/?p=" . $player['name'] . "&id=cheatometer\"target=\"_blank\">Check</a></td>";
                echo '</tr>';
            }
            echo '</tbody>';
            echo '</table>';
            echo '</div>';

我该怎么做呢?如果有人知道如何或理解我想要做的事情。

4 个答案:

答案 0 :(得分:1)

您需要更改以下代码

 foreach($serverplayer['serverinfo'][0]['players'] as $admname) {
                //$Alist[] = $admname['name']; // there is no need to take this array
                if(in_array($admname['name'], $admins)) {
                    $adminlist[] = $admname['name']; // take this array because may be more than one admins are there
                    $countAlist = count($adminlist);
                }
            }
            // Ends Admin List/Count
            // Everything below is just the data
            echo '<div class="four-columns six-columns-tablet twelve-columns-mobile">';
            echo '<pre>';
            echo implode(",",$adminlist);// you can print all admins as comma separated
            echo '</pre>';

答案 1 :(得分:0)

我认为你应该这样试试

$admins = array("INFIDEL_HARE", "Romania191", "mardog104", "INFIDEL_JIHAD", "stvnsng", "HellKnightFire", "McThump", "INFIDEL_JByrns93", "INFIDEL_ALPHA", "s0ck37", "Zepppster", "xRingmasteressx", "FreeeKillz", "smokert", "Ccls2", "INFIDEL_GRIF", "cuzco2585", "Prophet731", "KittensDrunk", "Rexperience-EF", "Whickerbasket", "InfamousHoole", "Cruz_5326", "-Grimreaperx9-", "JoeyT2");
            foreach($serverplayer['serverinfo'][0]['players'] as $admname) {
                $Alist = $admname['name'];//No need of saving to array
                if(in_array($Alist, $admins)) {
                   $adminlist[] = $admname['name'];//Save all the matching values to an array
                    $countAlist = count($adminlist);

                }
            }

答案 2 :(得分:0)

$Alist[] = $admname['name'];
            if(in_array($Alist, $admins)) {
                $adminlist = $admname['name'];
                $countAlist = count($adminlist);
            }

很奇怪:你在另一个数组($ admins)中寻找一个数组($ Alist)。

我认为你应该写的是

 if(in_array($admname['name'], $admins))

更多

 $adminlist = $admname['name'];
 $countAlist = count($adminlist);

不代表一件事:你在计算字符串(名称),而不是数组......

答案 3 :(得分:0)

听起来像是array_intersect()的工作:

array_intersect - 计算数组的交集
<?php
foreach( array_intersect(playersFromLog(), playersAdmins()) as $admin) {
    echo $admin, "\n";
}

function playersFromLog() { 
    return array( 'A', 'B', 'C', 'E', 'F', 'G', 'H', 'I', 'J', 'X', 'Z' );
}

function playersAdmins() {
    return array( 'D', 'E', 'K', 'X', 'Y', 'Z' );
}

打印

E
X
Z