Y
Y
Yapryntsew2015-12-06 00:19:49
PHP
Yapryntsew, 2015-12-06 00:19:49

How to get array elements with specific value?

There is an array like:

Array
(
    [45] => Array
        (
            [id] => 45
            [title] => Женская
            [parent_id] => 0
            [link] => 
            [sex] => 0
            [visibility] => 1
        )

    [46] => Array
        (
            [id] => 46
            [title] => Мужская
            [parent_id] => 0
            [link] => 
            [sex] => 0
            [visibility] => 1
        )

    [47] => Array
        (
            [id] => 47
            [title] => Аксессуары
            [parent_id] => 0
            [link] => 
            [sex] => 0
            [visibility] => 1
        )
        и т.д
)

How to get array elements where parent_id is 0 ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
moondog, 2015-12-06
@moondog

$array = array_filter($array, function($item) {
    if ($item['parent_id'] === 0) {
        return $item;
    }
}

S
Stalker_RED, 2015-12-06
@Stalker_RED

$filtered = array_filter($data, function($item) {
    return $item['parent_id'] === 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question