0

I have an array (paint) where the output looks like this:

array(3) {
   ["id"]=> string(1) "2"
   ["type"]=> string(1) "O"
   ["number"]=> string(1) "1"
}

array(3) {
   ["id"]=> string(1) "3"
   ["type"]=> string(1) "W"
   ["number"]=> string(1) "3"
}
array(3) {
   ["id"]=> string(1) "4"
   ["type"]=> string(1) "W"
   ["number"]=> string(1) "5"
}

Etc.

What I am trying is how often the type field contains W. So in this case I should have an output of 2. He should skip the first because there the type field is O.

What I tried:

 $sum = count($paint["type=W"]);

That doesn't work though. Is it possible to just count how often the type value is W?

5
  • 1
    $sum = count($paint["type=W"]); maybe a little typo just insert the ] Commented Aug 30, 2017 at 11:05
  • @North-Wind: That might not contain the syntax error but will still not do what Robbert wants. Commented Aug 30, 2017 at 11:06
  • I edited this in my first post. But that is still not working. Commented Aug 30, 2017 at 11:06
  • What is the relation between the subsequent arrays? Is it a 2 dimensional array? Are the arrays retrieved from something? Commented Aug 30, 2017 at 11:07
  • 1
    use array_column and array_count_values to get desired result. Commented Aug 30, 2017 at 11:07

4 Answers 4

1

Try using array_filter:

$typeW = array_filter($array1, fucntion($sub) {
    return $sub['type'] === 'W';
});
echo count($typeW);
Sign up to request clarification or add additional context in comments.

4 Comments

This solution assumes it is a 2D array, we don't know that.
Otherwise the question does not make much sense at all :(
The arrays could be fetched rows from a database. Fetched one by one.
@Robbert: Ah, so it is a 2D array after all, thanks for letting us know.
1

Hope this simple one will be helpful. Here we are using array_column on array to get all the values having key as type and then counting values using array_count_values.

Try this code snippet here

$result=array_count_values(array_column($array,"type"));
print_r($result["W"]);

Output: 2

Comments

0

What about a simple for loop?

 var count = 0;
 for (var i =0; i< array.length; i++) {
      if($paint[i]['type'] === 'W'){
           count++;
      }
 }

1 Comment

Now this assumes a 2D array? That information is not in the question.
0

Using a while loop:

<?php
$paint = array();
$i=0;
$x=0;
while($i<count($paint)){
    if($paint[$i][1]=="W"){
        $x++;
    }
    $i++;
}

echo "result: $x"; --> 2

3 Comments

This is just ugly, why not use a foreach loop?
That's a matter of taste, my friend.
@t1gor: Yes, it is, but also experience. A foreach loop has a build-in counter, you cannot got wrong with that. With a while loop you might get lost in the $x++; - $i++;, mixing them up and cause a difficult bug. A while loop should be used in cases were you don't have a simple counter, even a for loop would be better here because it keeps the counter together in one place.

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.