开发者

Is it possible to delete entire array based on presence of one value without rebuilding array?

开发者 https://www.devze.com 2023-03-11 21:55 出处:网络
I\'ve two arrays, the first ($needles) containing a bunch of Objects, the second ($stack) containing a bunch of key/values, but where one value is an array of Objects similar to the first array.

I've two arrays, the first ($needles) containing a bunch of Objects, the second ($stack) containing a bunch of key/values, but where one value is an array of Objects similar to the first array.

How can I delete all Objects within the target_p value in $stack where c_id matches any of the objects in $needle?

Thanks

Array ($needles)
(
    [0] => stdClass Object
        (
            [c_id] => 305164
            [neg] => 
            [seconds] => 604800
            [f_min] => 10
        )

    [1] => stdClass Object
        (
            [c_id] => 305165
            [neg] => 
            [seconds] => 604800
            [fr_min] => 10
        )

    [2] => stdClass Object
        (
            [c_id] => 305166
            [neg] => 
            [seconds] => 604800
            [f_min] => 10
        )

)
*****************
Array ($stack)
(
    [req_all] => 
    [target_p] => Array
        (
            [0] => stdClass Object
                (
                    [c_id] => 305164
                    [pid] => 2323554
                    [neg] => 
                    [seconds] => 
                    [f_min] => 
                )

            [1] => stdClass Object
                (
                    [c_id] => 305165
                   开发者_StackOverflow社区 [pid] => 1964608
                    [neg] => 
                    [seconds] => 
                    [f_min] => 
                )

            [2] => stdClass Object
                (
                    [c_id] => 305166
                    [neg] => 1
                    [seconds] => 604800
                    [f_min] => 
                )

            [3] => stdClass Object
                (
                    [c_id] => 305167
                    [neg] => 1
                    [seconds] => 604800
                    [f_min] => 
                )

            [4] => stdClass Object
                (
                    [c_id] => 314022
                    [pid] => 4950148
                    [neg] => 
                    [seconds] => 
                    [f_min] => 
                )

        )

    [logical_e] => 
)

Desired output:

Array ($stack)
(
    [req_all] => 
    [target_p] => Array
        (
            [0] => stdClass Object
                (
                    [c_id] => 305167
                    [neg] => 1
                    [seconds] => 604800
                    [f_min] => 
                )

            [1] => stdClass Object
                (
                    [c_id] => 314022
                    [pid] => 4950148
                    [neg] => 
                    [seconds] => 
                    [f_min] => 
                )

        )

    [logical_e] => 
)


Without rebuilding an array? Not really. But it's still easy.

Rebuild the needles array with keys matching the c_id values:

$o_needles = array();
foreach ($needles as $needle) {
    $o_needles[$needle->c_id] = $needle;
}

Then remove any nodes in the stack that has a c_id value matching a key in the modified needles array:

foreach ($stack['target_p'] as $key => $obj) {
    if (array_key_exists($obj->c_id, $o_needles)) {
        unset($stack['target_p'][$key]);
    }
}

The performance/complexity is O(n + m).

If you were to use a function like array_map, array_walk, array_filter or a nested foreach, you'd have to look over each of the needles for each element in the stack, which would be less efficient, especially with large datasets (O(n * m)).


By using unset() ?.

I do not think that I have clearly understood what exactly you need and therefore 2 examples (You really should provide the desired result, so we can check if we understood you correctly).

To delete array element from $stack['target_p'] if c_id of such element matches the same on $needles array:

foreach ($stack['target_p'] as $k => $v)
{
    foreach ($needles as $needle)
    {
        if ($v->c_id == $needle->c_id) {
            unset($stack['target_p'][$k]);
        }
    }
}

To delete all array elements from $stack['target_p'] if any c_id matches the same on $needles array:

foreach ($stack['target_p'] as $k => $v)
{
    foreach ($needles as $needle)
    {
        if ($v->c_id == $needle->c_id) {
            $stack['target_p'] = array(); // will delete elements but preserver target_p key
            // or
            unset($stack['target_p']); // will delete elements & target_p key

            break 2; // exit from both foreach loops
        }
    }
}


Use array_filter().

0

精彩评论

暂无评论...
验证码 换一张
取 消