数组的定位

是否可以根据特定的数值数组来定位数组值?我有这张表:


+---------+-------+-------+------------+---------------------+------------------------------+

| deck    | count | break | cards      | donator             | image                        |

+---------+-------+-------+------------+---------------------+------------------------------+

| prejoin | 20    | 5     | 16, 01, 05 | User2, User1, User7 | img2.jpg, img1.jpg, img7.jpg |

+---------+-------+-------+------------+---------------------+------------------------------+

这就是我到目前为止所拥有的: https: //shizen.hakumei.org/test.php

它仅显示 User7 的名称和图像链接,我计划显示框 01 表示 User1 的名称和图像链接,然后显示框 16 表示 User2 的名称和图像链接。所以基本上就像:


Box 16 = User2 (img2.jpg link)

Box 01 = User1 (img1.jpg link)

Box 05 = User7 (img7.jpg link)

这是我当前必须从给定链接显示表格的代码:

function trim_value(&$value) { $value = trim($value); }

$res = $database->query("SELECT * FROM `table` ORDER BY `deck` LIMIT 1");

while ( $col = mysqli_fetch_assoc($res) ) {

    echo '<center>';

    $cards = explode(',', $col['cards']);

    array_walk($cards, 'trim_value');

    if ( $col['cards'] == '' ) { $count = 0; } else { $count = count($cards); }


    $donator = explode(',', $col['donator']);

    $donator = array_map(trim, $donator);

    foreach ( $donator as $usr ) { $usr = trim($usr); }


    $image = explode(',', $col['image']);

    $image = array_map(trim, $image);

    foreach ( $image as $img ) { $img = trim($img); }


    echo '<h2>'.$col['deck'].' ('.$count.' / '.$col['count'].')</h2>

    <table width="625" cellspacing="0" border="1"><tr>';

    for ( $i = 1; $i <= $col['count']; $i++ ) {

        $number = $i;

        if ( $number < 10 ) { $number = "0$number"; }

        $card = $number;

        if ( in_array($card, $cards) ) echo '<td width="125" align="center" height="105">'.$card.'<br ><a href="'.$img.'" target="_blank">'.$usr.'</a></td>';

        else { echo '<td width="125" align="center" height="105">00</td>'; }

        if ( $col['break'] !== '0' && $i % $col['break'] == 0 ) { echo '</tr>'; }

    }

    echo '</table>

}

提前谢谢了!如果不可能或者有更好的方法,我也愿意接受想法。:)


MMTTMM
浏览 93回答 2
2回答

HUWWW

提示:您不需要为代码中的空间支付租金,因此您应该使用它来获得更好的可浏览性。我已将您的用户名和图像放入基于密钥的数组中,因此您可以查找该密钥是否存在,如果不存在,则不需要用户名或图像。这里的问题是你在表中的存储,你使用以 0 开头的数字。您还应该考虑这一点:删除列卡、图像、捐赠者并将所有值放在不同的表中,例如:牌组:预加入,card_id:16,用户:17,图片:img12.jpgwhile ( $col = mysqli_fetch_assoc($res) ) {&nbsp; &nbsp; $data = array();&nbsp; &nbsp; if ($col['cards'] == '' ){&nbsp; &nbsp; &nbsp; &nbsp; $cards = explode(',', $col['cards']);&nbsp; &nbsp; &nbsp; &nbsp; array_walk($cards, 'trim_value');&nbsp; &nbsp; &nbsp; &nbsp; $count = count($cards);&nbsp; &nbsp; &nbsp; &nbsp; $donator = explode(',', $col['donator']);&nbsp; &nbsp; &nbsp; &nbsp; $image = explode(',', $col['image']);&nbsp; &nbsp; &nbsp; &nbsp; foreach ($cards as $key=>$card){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data[$card] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user' => trim($donator[$key]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'img' => trim($image[$key])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; echo '<h2>'.$col['deck'].' ('.$count.' / '.$col['count'].')</h2>&nbsp; &nbsp; <table width="625" cellspacing="0" border="1"><tr>';&nbsp; &nbsp; for ( $i = 1; $i <= $col['count']; $i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; $number = $i;&nbsp; &nbsp; &nbsp; &nbsp; if ( $number < 10 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$number = "0".$number;&nbsp; &nbsp; &nbsp; &nbsp; if ( in_array($i, $data) )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<td width="125" align="center" height="105">'.$i.'<br ><a href="'.$data[$i]['img'].'" target="_blank">'.$data[$i]['user'].'</a></td>';&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<td width="125" align="center" height="105">00</td>';&nbsp; &nbsp; &nbsp; &nbsp; if ( $col['break'] !== '0' && $i % $col['break'] == 0 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</tr>';&nbsp; &nbsp; }&nbsp; &nbsp; echo '</table>';}

呼唤远方

explode返回一个数组,通过使用foreach(... $key => ...)我们将获得 中当前元素的键$key。由于所有数组共享相同的键,因此我们可以使用$keyof $cardsfor$users和$images。$cards = explode(',', '16, 01, 05');$users = explode(',', 'User2, User1, User7');$images = explode(',', 'img2.jpg, img1.jpg, img7.jpg');$cards = array_map('trim', $cards);$users = array_map('trim', $users);$images = array_map('trim', $images);foreach ($cards as $key => $value) {    echo sprintf(        'Box %02d = %s (%s link)',        $cards[$key],        $users[$key],        $images[$key]    ) . PHP_EOL;}输出Box 16 = User2 (img2.jpg link)Box 01 = User1 (img1.jpg link)Box 05 = User7 (img7.jpg link)工作示例。
打开App,查看更多内容
随时随地看视频慕课网APP