/    Sign up×
Community /Pin to ProfileBookmark

Counting an item – multidimensional array

I’m still kinda new at this, but I have a multidimensional array (I think). It is an array that has arrays inside it. The inside arrays all have the same structure, (qty, product, color, size). How do I count how many items are ‘White’ * qty? I looked into the php manual and found array_count_values but others say it doesn’t work with MD arrays. ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@UnexplainedWaysOct 24.2005 — It's it a multidimensional or an array with an array inside of it?
Copy linkTweet thisAlerts:
@LocalHeroauthorOct 24.2005 — Hmmm... As I said, I'm still new. That's why I thought to describe it to see. My array is like this:

array(

[0] array([0]qty, [1]product, [2] color, [3] size)

[1] array([0]qty, [1]product, [2] color, [3] size)

[2] array([0]qty, [1]product, [2] color, [3] size)

[3] array([0]qty, [1]product, [2] color, [3] size)

[4] array([0]qty, [1]product, [2] color, [3] size)

[5] array([0]qty, [1]product, [2] color, [3] size)

[6] array([0]qty, [1]product, [2] color, [3] size)

...)

I'm not sure if that's the right syntax, but i hope the idea comes accross. In array[2] of each array I need to count 'White' occurances and times it by the qty of that same array. If this is still fuzzy let me know. Thanks!
Copy linkTweet thisAlerts:
@NogDogOct 24.2005 — One way:
[code=php]
<?
$data = array(array('qty' => 1,
'product' => 'product1',
'color' => 'white',
'size' => 'L'),
array('qty' => 1,
'product' => 'product2',
'color' => 'red',
'size' => 'S'),
array('qty' => 2,
'product' => 'product3',
'color' => 'white',
'size' => 'M'),
array('qty' => 2,
'product' => 'product4',
'color' => 'blue',
'size' => 'XL'),
array('qty' => 3,
'product' => 'product5',
'color' => 'white',
'size' => 'M'));
# get total quantity of white-colored items:
$count = 0;
foreach($data as $item)
{
if($item['color'] == 'white')
{
$count += $item['qty'];
}
}
echo "<p>Total: $count</p>n";
[/code]
Copy linkTweet thisAlerts:
@LocalHeroauthorOct 24.2005 — Thanks!
×

Success!

Help @LocalHero spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...