3

I'm using wordpress and based off the post I have an array of cards, each post will have a different number of cards in this array and I was wondering how I can show the number of cards in the array.

$class_cards[]=array('card_count'=>$card_count);

The total number of cards in this array will vary by post, so I want to be able to store the number of cards in a variable so I can echo it out later down the page.

Edit: Follow Up Question

I have a follow up to this question, I have a variable which is called $card_count which is stored in the array as you can see above. The card count will always be 1 or 2 saying that there is either one version of this card or two versions of this card. If there are two I would need to count that card twice in the overall number how can I do this with count($class_cards)?

For example lets say I have a total of 9 cards in that array. The count would turn out as 9, but in that array 6 out of those 9 cards have 2 as their card count, while 3 has 1 as their card count. So the total number of cards should be 15 instead of 9.

5
  • 4
    count($class_cards) ? Commented Jan 31, 2015 at 5:18
  • mihai, please put that as an answer so the question does not go unanswered :) Commented Jan 31, 2015 at 5:23
  • @GavinSimpson thanks for the heads up but it seems someone else took care of this :) Commented Jan 31, 2015 at 13:32
  • Lol, Copy someone's comment into an answer. Unlucky. Commented Jan 31, 2015 at 13:35
  • @mihai would you be able to help with the follow up question in the edit? Thanks Commented Jan 31, 2015 at 14:43

2 Answers 2

4

You can do it in very simple way like

$count_cards = count($class_cards);

This will return you the count of the cards.

Sign up to request clarification or add additional context in comments.

1 Comment

I added an edit to the question with a follow up, would you be able to help with that?
0

Hi i think this piece of code will help you if i got what you want

$count_cards = count($class_cards); // Total number of objects(cards) in an array in this  example  it  is 9 

foreach($class_cards AS $key => $value)
{
   //Assuming the card count as 9  this  loop will  run 9 times 
   if($value['card_count'] == 2)
   {
      $count_cards++; // Adding another card  if the card  count is  2
   }
}
echo $count_cards ; // Total number of cards , Should  echo 15 according to this example

7 Comments

What would be the variable with the stored value of 16 in it then?
@Greenhoe You said $card_count will only be 1 or 2 --> "The card count will always be 1 or 2 saying that there is either one version of this card or two versions of this card."
Correct, but where is the total card count being added up in? So if we had a total of 9 cards with 6 of them being card count of 2 and 3 being a card count of 1, we should have a total of 15 cards, I need to be able to have 15 in a variable so I can display that later on in the post. I see how you are checking the card count, but not sure I see where the total card count is being stored.
@Greenhoe edited the answer with more comments hope you get what i am doing :)
It still echo's 9, I think where this is going wrong is I don't see where $card_count variable is used to see if it is 2 cards or 1.
|

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.