Home > Mobile >  array filter of PDO result
array filter of PDO result

Time:10-31

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]

CodePudding user response:

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);
  • Related