khalid seleem
khalid seleem

Reputation: 117

array filter of PDO result

I get PDO rows array which contains the result:

parent_id , item_id

NULL        2
NULL        3
1           5
1           8

I want a new array where parent_id is not NULl

Means

new arr=[5,8]

Upvotes: 0

Views: 106

Answers (1)

Tural Rzaxanov
Tural Rzaxanov

Reputation: 781

You need to set new array from exits or to duplicate request with IS NULL condition. With array method, your code will show like this:

$arr = [
    [
        'parent_id' => null,
        'item_id' => 2,
    ],
    [
        'parent_id' => null,
        'item_id' => 4
    ],
    [
        'parent_id' => 2,
        'item_id' => 20,
    ],
];

$new_arr = array_filter($arr,function ($item) {
    return !$item['parent_id'];
});

print_r($new_arr);

Upvotes: 1

Related Questions