Y
Y
Yaroslav Studenikin2020-08-26 15:18:44
PHP
Yaroslav Studenikin, 2020-08-26 15:18:44

Sampling from an array by value?

Hello!
I have an array like this:

[0] => Array
        (
            [good] => 1
            [status] => 0
        )

    [1] => Array
        (
            [good] => 2
            [status] => 0
        )

    [2] => Array
        (
            [good] => 3
            [status] => 0
        )

    [3] => Array
        (
            [good] => 4
            [status] => 0
        )

    [4] => Array
        (
            [good] => 5
            [status] => 0
        )

    [5] => Array
        (
            [good] => 6
            [status] => 0
        )

    [6] => Array
        (
            [good] => 7
            [status] => 0
        )

    [7] => Array
        (
            [good] => 8
            [status] => 0
        )

    [8] => Array
        (
            [good] => 9
            [status] => 0
        )

    [9] => Array
        (
            [good] => 10
            [status] => 0
        )

    [10] => Array
        (
            [good] => 11
            [status] => 0
        )

The task is to get, for example, 5 arrays from it, in which status => 0. (There are records with status => 1, status => 2, etc.)
I tried to make such a selection on yii2, I just can’t figure it out. used ArrayHelper.
Tell me what method to select a certain number of records from the array by the desired value of the status key.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexalexes, 2020-08-26
@yar_stun

Filter the array:
$selected_items = array_filter($inp_array, function($item){return $item['status'] == 0;});

F
FanatPHP, 2020-08-26
@FanatPHP

foreach + if

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question