1

Let's say I have an array like this:

Array
(
    [id] => 1
    [color_object] => blue
    [color_1] => green
    [color_2] => blue
    [color_3] => blue
    [color_4] => red
    [color_5] => purple
    [AltColor] => blue
)

I can use array_count_values and it will give me the count for blue as 4.

But I need to only count the values within specific keys of the array, in this example I need to limit my count to the color_1 color_2 color_3 color_4 and color_5 only.

Is it possible to only count the instances of blue within specified keys, giving me a desired result of 2?

PHP code:

$array = array("id"=>"1", "color_object"=>"blue", "color_1"=>"green", "color_2"=>"blue", "color_3"=>"blue", "color_4"=>"red", "color_5"=>"purple", "AltColor"=>"blue");
12
  • Make another array with just those keys. Commented Dec 15, 2016 at 3:03
  • Why do you have separate color_1, color_2, etc. elements? Why not make it an array 'colors' => array('green', 'blue', 'blue', 'red', 'purple', 'blue') Commented Dec 15, 2016 at 3:09
  • In general, whenever you find yourself creating numbered variables or keys like that, you should probably be using an array instead. Commented Dec 15, 2016 at 3:11
  • The actual array I am working with is one returned from mysqli_fetch_assoc. I was trying to minimize the question, and I don't think I have as much control over the array as my question implied. Can you suggest a function reference to look into? I think maybe creating another array with just those keys might work in this instance. Commented Dec 15, 2016 at 3:16
  • I just supplied the manually created PHP code so I could provide a code example to work with. Commented Dec 15, 2016 at 3:17

2 Answers 2

2
<?php

    $array = array(
        "id"=>"1",
        "color_object"=>"blue",
        "color_1"=>"green",
        "color_2"=>"blue",
        "color_3"=>"blue",
        "color_4"=>"red",
        "color_5"=>"purple",
        "AltColor"=>"blue"
    );

    $preg = '/^color_\d{1,}/i';

    $callback = function ($val) use($preg) {
        if (preg_match($preg, $val, $match)) {
            return true;
        } else {
            return false;
        }   
    };

    $data = array_filter($array, $callback, ARRAY_FILTER_USE_KEY);
    $result = array_count_values($data);

    var_dump($result);
Sign up to request clarification or add additional context in comments.

3 Comments

array_filter($array, $callback, ARRAY_FILTER_USE_KEY); ARRAY_FILTER_USE_KEY php version >= 5.6
Thank you! I'm still maybe looking for something simpler, though. Going to have to do some more research and work on this tomorrow I will accept an answer then.
Actual solution I used is in the comments of my original post, but I'm going to accept your answer as you were the first to provide a solution to the broader question.
1

You can just use function array_sum wrap your array_map inside it to add just return indexes that meet your condition

$array = Array
(
    'id' => 1,
    'color_code' => 'blue',
    'color_1' => 'green',
    'color_2' => 'blue',
    'color_3' => 'blue',
    'color_4' => 'red',
    'color_25' => 'purple',
    'AltColor' => 'blue',
);

echo "The total count is of index => color and value => blue is ".array_sum(array_map(
    function($value, $key) {
        return (preg_match('/^color_\d{1,}/i', $key) && $value == 'blue') ? true : false;
    }, $array, array_keys($array))
);

Demo

Comments

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.