如何循环数组并添加到最后

时间:2017-01-18 05:55:53

标签: php arrays

经过几个小时的努力解决这个问题,我希望能来找你们帮忙!我想要做的是每个牌组(由$filename声明)我想创建一个阵列,其中包含该牌组中的所有牌(每个牌组中有15张牌)。然后将会员的收集套牌与全套卡片进行比较,然后将我的交易套牌与他们的"仍然需要"阵列。阵列/代码效果很好......如果我只想比较一个牌组。

我无法弄清楚如何保持循环中的值并将更多内容添加到数组中而不是覆盖它。我喜欢在所有套牌中列出所有会员需要的牌,而不仅仅是一张牌。

我已经尝试了array_pusharray_merge的版本,但我感到茫然。非常感谢任何帮助!!!

$select3=mysqli_query($connect, "SELECT * FROM `cards` ORDER BY `filename` ASC");
while($row3=mysqli_fetch_assoc($select3)) {
    // pulls all cards from database
    $filename=$row3['filename'];
    $deckname=$row3['deckname'];
    $cardcount=$row3['count'];
    $count=substr_count($collecting, $filename);

    $expcol=explode(", ", $collecting, -1); //puts member collecting deck into array
    $exptra=explode(", ", $trading, -1); //puts my trading deck into array

    if($count>0) {
        $cardarray=array($filename."01", $filename."02", $filename."03", $filename."04", $filename."05", $filename."06", $filename."07", $filename."08", $filename."09", $filename."10", $filename."11", $filename."12", $filename."13", $filename."14", $filename."15",); // creates array of all cards in specific card deck


        $stillneed=array_diff($cardarray,$expcol); //shows cards other member still needs from the deck
        $res=array_intersect($stillneed, $exptra); //shows cards I have that the other member still needs
        $countres=count($res); // counts how many cards I have that other member needs

        if($countres>0) {
            $check="✓"; // if I have a card to give, create checkmark
        } else {
            $check="";
        }
    }
}   

更具体地说,假设我的数据库中只有两行

newdeck

testdeck

我想创建一个包含newdeck01,newdeck02,newdeck03,testdeck01,testdeck02,testdeck03的数组。当我使用上面的代码时,它会抓取testdeck,它会清除数组中的所有newdeck项。一次只能使用一个甲板,但我想在同一个阵列中同时列出所有甲板。我希望这是有道理的。

修改

我想我做到了!!这是我更新的代码。

$select3=mysqli_query($connect, "SELECT * FROM `cards` ORDER BY `filename` ASC");
$cardarray=array();
while($row3=mysqli_fetch_assoc($select3)) {
    // pulls all cards from database
    $filename=$row3['filename'];
    $deckname=$row3['deckname'];
    $cardcount=$row3['count'];
    $count=substr_count($collecting, $filename);

    $expcol=explode(", ", $collecting, -1); //puts member collecting deck into array
    $exptra=explode(", ", $trading, -1); //puts my trading deck into array

    if($count>0) {
        for($i=1;$i<=$cardcount;$i++) {
            if($i<10){
                $in="0".$i;
            } else {
                $in=$i;
            }
            $items=$filename.$in;
            $cardarray[] = $items; // creates array of all cards in specific card deck
        }
    }
}   

$stillneed=array_diff($cardarray,$expcol); //shows cards other member still needs from the deck
$res=array_intersect($stillneed, $exptra); //shows cards I have that the other member still needs
$countres=count($res); // counts how many cards I have that other member needs

if($countres>0){
    $check="✓"; // if I have a card to give, create checkmark
} else {
    $check="";
}

1 个答案:

答案 0 :(得分:0)

  

你可以在php中合并两个数组,如果你想要所有卡在同一个数组中。

`$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b);`