0

I'm workking on a shortcode for wordpress and did a loop and stored a few variables in an array some of my posts won't have anything in the array and i'm getting an error for the ones that do.

My two arrays are

$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);

I then output them with the code below, the issue I'm having is with most posts we have values stored in both arrays and output them and everything works great, but if we have values stored in just the $class_cards array and nothing in the neutral cards array I get the error

Warning: Invalid argument supplied for foreach() in /home/hsp/public_html/wp-content/themes/hsp/content/deck-info.php on line 264

The output looks like this, so what I'm asking is how can I check to make sure there is something in those arrays to fix this error

    // 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;
}
2
  • I can't see $neutral_cards in your code. Please post line 264 of /home/hsp/public_html/wp-content/themes/hsp/content/deck-info.php Commented Feb 23, 2015 at 4:57
  • Try to print_r($class_cards ); before foreach($class_cards as $row=>$value) if it is the line 264. $class_cards definitely empty or not set at all. Commented Feb 23, 2015 at 5:06

3 Answers 3

2

Check this one

$Array = array_filter($yourArray );
if (!empty($Array)) {
echo "array not empty";
}else{
echo "array is empty";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use php count() function to do that

    if(count($class_cards) && count($neutral_cards)){
//foreach loop....
}

1 Comment

I think !empty is better.
0

What about something like this?

if (!empty($class_cards))
{
    foreach($class_cards as $row=>$value)
    { 
        ....
    }
}

....

if (!empty($neutral_cards))
{
    foreach($neutral_cards as $row=>$value)
    { 
        ....
    }
}

This will verify the array exists and that it isn't empty before trying to loop through it.

2 Comments

I thought empty checked if it existed, no?
It checks if it exists, but doesn't care if it's an array or not. But, I can see in the question that the OP is declaring the array outright so looks like is_array is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.