0

I have this array

Array
(
    [0] => Array
        (
            [status] => 15
            [orario] => 10:00
        )

    [1] => Array
        (
            [status] => 15
            [orario] => 11:00
        )

    [2] => Array
        (
            [status] => 15
            [orario] => 09:00
        )

    [3] => Array
        (
            [status] => 12
            [orario] => 09:00
        )

    [4] => Array
        (
            [status] => 12
            [orario] => 09:00
        )
    [5] => Array
        (
            [status] => 12
            [orario] => 10:00
        )

)

So I want a new array where I have the count of similar status, in this case, status=12, I have 2 values with status 12 and orario = 09:00, 1 case with status=12 and orario = 10:00

Array
(
    09:00 => 2
    10:00 => 1
)

with array_column I have no filter

$stat_orario = array_column($the_array, 'status', 'orario');
print_r($stat_orario);

So I have the total result

Array
(
    09:00 => 3
    10:00 => 2
    11:00 => 1
)
4
  • 2
    array_column + array_count_values Commented Mar 3, 2020 at 17:54
  • Please add some of the code that you have written to try and answer your own question. Commented Mar 3, 2020 at 18:13
  • Hi, thanks but how can I filter only some [status]. If I use array_column I have no filter. Commented Mar 3, 2020 at 18:14
  • @geeves I have a loop to populate the array for others functions. So I don't want change the loop because I need some values after. Commented Mar 3, 2020 at 18:19

2 Answers 2

2

You should try following code :

$arr = array(
        array('status'=>15,'orario'=>'10:00'),
        array('status'=>15,'orario'=>'11:00'),
        array('status'=>15,'orario'=>'09:00'),
        array('status'=>12,'orario'=>'09:00'),
        array('status'=>12,'orario'=>'09:00'),
        array('status'=>12,'orario'=>'10:00')
    );

    $orario = array_count_values(array_column($arr, 'orario'));

Output is :

Array
(
    [10:00] => 2
    [11:00] => 1
    [09:00] => 3
)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, but I want to filter with only status = 12
0

This is the solution I find thanks to Forge Web Design

$arr = array(
        array('status'=>15,'orario'=>'10:00'),
        array('status'=>15,'orario'=>'11:00'),
        array('status'=>15,'orario'=>'09:00'),
        array('status'=>12,'orario'=>'09:00'),
        array('status'=>12,'orario'=>'09:00'),
        array('status'=>12,'orario'=>'10:00')
    );

// I transform the array with only status = 12, but I have to do this for every status I want to extract values

foreach ($arr as $key => $val) {
    if ($arr[$key]['status'] == '12')
        $status_12[$key] = $val;
}
    $orario = array_count_values(array_column($arr, 'orario'));

Output now is only status = 12

Array
(
    [10:00] => 1
    [09:00] => 2
)

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.