0

I'm trying to count the number of times a certain value turns up in an array. Except this value will always increment by 1, and there is an unknown number of these values in the array.

Example:

$first = array( 'my-value-1','my-value-2','my-value-3' );

$second = array( 'my-value-1','my-value-2','my-value-3', 'my-value-4', 'my-value-5' );

My goal is to be able to retrieve a count of 3 for $first and a count of 5 for $second in the example above.

There may be other values in the array, but the only values I'm interested in counting are the ones that start with my-value-.

I won't know the number of the values in the array, but they will always start with my-value- with a number added to the end.

Is there a way to count the number of times my-value- shows up in the array with some sort of wildcard?

4
  • This seems like a simple combination of a foreach loop or array_map and strpos Commented Nov 16, 2016 at 19:36
  • Standard PHP count($arrayname) give you how many occurances there are in an array Commented Nov 16, 2016 at 19:42
  • 1
    If you have something in the array that does not start with my-value- then it would have been useful to show that in the samples you provide Commented Nov 16, 2016 at 19:45
  • 1
    If you ask a good question there is more likelyhood of getting a good answer and we dont waste our time Commented Nov 16, 2016 at 19:55

3 Answers 3

7

Use a regex to filter the array and count the values that match. You could also use ^ to force it to be at the beginning ^my-value-\d+:

$count = count(preg_grep('/my-value-\d+/', $first));

You could also do it this way. Again you could use === 0 instead to make it match at the beginning:

$count = count(array_filter($first, function($v) {
                                        return strpos($v, 'my-value-') !== false;
                                    }));
Sign up to request clarification or add additional context in comments.

Comments

0

A quick function to count values by providing a partial string and the target array.
Note: search is case-insensitive.

function countValues($prefix, $array) {
    return count(array_filter($array, function($item) use ($prefix) {
        return stripos($item, $prefix) !== false;
    }));
}

Usage:
$count = countValues('my-value', $first);

Comments

-1

According to your question, "they will always start with my-value- with a number added to the end." So you don't need a regex, just a count of the number of items in your array, using PHP's built-in count() function. Try:

<?php 
$first = array( 'my-value-1','my-value-2','my-value-3' );
$second = array( 'my-value-1','my-value-2','my-value-3', 'my-value-4', 'my-value-5' ); 

$size_of_first = count($first);
$size_of_first = count($first);

echo $size_of_first; //Will echo 3
echo $size_of_first; //Will echo 5

?>

4 Comments

There may be other values
Yes, this is the issue I'm facing is that there will be other values. The values I'm interested in counting will always start with my-value- though. I will clarify the original question.
@ScottD Well then show that possibility in your question. We are not clairvoyant we can only answer based on what is in the question
Then go with @AbraCadaver 's solution.

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.