我正在使用wordpress的短代码并做了一个循环并在数组中存储了一些变量我的一些帖子在数组中没有任何东西,而且我收到的错误就是那些。 / p>
我的两个数组是
$class_cards[]=array('card_count'=>$card_count,'has_term'=>$has_term,'card_img_class'=>$card_img_class,'card'=>$card,'card_title'=>$card_title,'card_mana'=>$card_mana,'card_slug'=>$card_slug);
$neutral_cards[]=array('card_count'=>$card_count,'has_term'=>$has_term,'card_img_class'=>$card_img_class,'card'=>$card,'card_title'=>$card_title,'card_mana'=>$card_mana,'card_slug'=>$card_slug);
然后我用下面的代码输出它们,我遇到的问题是大多数帖子我们将值存储在两个数组中并输出它们并且一切都很好,但是如果我们将值存储在$ class_cards数组中并且中性卡阵列中没有任何内容我得到了错误
警告:在第264行的/home/hsp/public_html/wp-content/themes/hsp/content/deck-info.php中为foreach()提供的参数无效
输出看起来像这样,所以我要问的是如何检查以确保这些数组中有某些东西来修复此错误
// Output Class Cards
$output .= '<div class="class-cards">';
$output .= 'Class Cards ('. $total_class_cards .')';
$output .= '<span class="sc-deck-mana-cost"><img src="http://hearthstoneplayers.com/img/icons/dust.png" />'.$crafting_cost.'</span>';
$i=0;
while($i<=20)
{
foreach($class_cards as $row=>$value)
{
if($value['card_mana']==$i)
{
$output .= '
<div class="card">
'. $value['card_count'] .'
<div class="card-overlay"></div>
<div '. $value['card_img_class'] .' ><img src="http://hearthstoneplayers.com/img/cards/'. $value['card'] .'.png"></div>
<div class="decklist-tooltip"><img src="http://hearthstoneplayers.com/img/cards/'. $value['card'] .'.png"></div>
<span class="card-title">'. $value['card_title'] .'</span>
<span class="mana-icon"><img src="http://hearthstoneplayers.com/img/icons/mana.png" /></span>
<span class="mana-cost">'. $value['card_mana'] .' </span>
<a class="dl-card-link" href="http://hearthstoneplayers.com/cards/'. $value['card'] .'/"></a>
</div>
';
}
}
++$i;
}
$output .= '</div>'; // Close Div class-cards
// Output Neutral Cards
$output .= '<div class="neutral-cards">';
$i=0;
while($i<=20)
{
foreach($neutral_cards as $row1=>$value1)
{
if($value1['card_mana']==$i)
{
$output .= '
<div class="card">
'. $value1['card_count'] .'
<div class="card-overlay"></div>
<div '. $value1['card_img_class'] .' ><img src="http://hearthstoneplayers.com/img/cards/'. $value1['card'] .'.png"></div>
<div class="decklist-tooltip"><img src="http://hearthstoneplayers.com/img/cards/'. $value1['card'] .'.png"></div>
<span class="card-title">'. $value1['card_title'] .'</span>
<span class="mana-icon"><img src="http://hearthstoneplayers.com/img/icons/mana.png" /></span>
<span class="mana-cost">'. $value1['card_mana'] .' </span>
<a class="dl-card-link" href="http://hearthstoneplayers.com/cards/'. $value1['card'] .'/"></a>
</div>
';
}
}
++$i;
}
答案 0 :(得分:2)
检查一下
$Array = array_filter($yourArray );
if (!empty($Array)) {
echo "array not empty";
}else{
echo "array is empty";
}
答案 1 :(得分:0)
使用php count()函数来执行此操作
if(count($class_cards) && count($neutral_cards)){
//foreach loop....
}
答案 2 :(得分:0)
这样的事情怎么样?
if (!empty($class_cards))
{
foreach($class_cards as $row=>$value)
{
....
}
}
....
if (!empty($neutral_cards))
{
foreach($neutral_cards as $row=>$value)
{
....
}
}
这将验证数组是否存在,并且在尝试循环之前它不是空的。