Tom
Tom

Reputation: 21

Filter a multidimensional array on multiple levels and return a column of data from qualifying subsets

I have below set of data

[{
    "sr_team": [{
        "classifications": "BPO",
        "email": "[email protected]",
        "names": "Maynard Gregory"
    }],
    "jr_team": [{
        "names": "Mr. Curtis Zemlak I",
        "email": "[email protected]",
        "available": false
    }, {
        "names": "Prof. Naomi Eichmann II",
        "email": "[email protected]",
        "available": true
    }]
}, {
    "sr_team": [{
        "classifications": "Tech",
        "email": "[email protected]",
        "names": "Olivia Dawson"
    }],
    "jr_team": [{
        "names": "Benjamin George",
        "email": "[email protected]",
        "available": true
    }]
}]

From the above data, I need the names of jr_team who have available as true. i.e. the code should return below two values.

[
    'Prof. Naomi Eichmann II',
    'Benjamin George'
]

Upvotes: 0

Views: 95

Answers (1)

jibsteroos
jibsteroos

Reputation: 1391

FWIW, you could also opt for a slightly more concise snippet to determine qualifying values.

Check out the demo:

$store = [];
foreach ($arr as $teams) {
    foreach ($teams['jr_team'] as $record) {
        if ($record['available']) {
            $store[] = $record['names'];
        }
    }
}

Result ($store)

Array
(
    [0] => Prof. Naomi Eichmann II
    [1] => Benjamin George
)

Upvotes: 1

Related Questions