Extracting a piece of an array based on a condition

I have a multi-dimensional array.
My goal is to check a specific key/value in them and if it meets a condition(this value) then this array member be removed from the multi-dimensional array and placed in another array.

So if our original multi-dimensional array had three members and one of them met the condition…this member would be removed leaving us a 2-members array and and creating and a second array which will have as member the removed member of the first array.

I do understand this is a multi-step process.So my first question is how to remove the member whose value meets the condition…

Try setting the array item to NULL

or use the unset() function.

Though unset() makes a variable NULL there is a difference in scope you should consider.
http://php.net/manual/en/function.unset.php

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

One possible solution is to first determine what are the conditions that will remove a member from the array. Then use those conditions to run the array through array_filter. This will return you an array that only contains the members to be removed. You can then pass the original array through array_filter again, but negate the filter that you pass in. The second array will then only contain those members that you want to retain. The example below may explain it better.

Making some huge assumptions about your multi-dimensional array.

<?php
/**
 * The array that we want to remove certain members from.
 */
$abba = [
    [
        'name' => 'Agnetha',
        'sex'  => 'Female'
    ],
    [
        'name' => 'Björn',
        'sex'  => 'Male'
    ],
    [
        'name' => 'Benny',
        'sex'  => 'Male'
    ],
    [
        'name' => 'Anni-Frid',
        'sex'  => 'Female'
    ]
];

/**
 * The conditions under which a member should be removed from the array.
 */
function filter($person) {
    return $person['name'] === 'Agnetha' && $person['sex'] === 'Female';
}

/**
 * Remove from the array the member(s) that match the condition.
 */
$removed = array_filter($abba, function ($person) {
    return filter($person);
});

/**
 * The remaining members of the array are simple those that don't match the condition.
 */
$remaining = array_filter($abba, function ($person) {
    return !filter($person);
});

print_r($removed);
print_r($remaining);

Array
(
    [0] => Array
        (
            [name] => Agnetha
            [sex] => Female
        )

)
Array
(
    [1] => Array
        (
            [name] => Björn
            [sex] => Male
        )

    [2] => Array
        (
            [name] => Benny
            [sex] => Male
        )

    [3] => Array
        (
            [name] => Anni-Frid
            [sex] => Female
        )
)
1 Like

yes that did it…thanks.

No problem, Happy to have helped.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.