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
[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_idvalues:Then remove any nodes in the stack that has a
c_idvalue matching a key in the modified needles array:The performance/complexity is O(n + m).
If you were to use a function like
array_map,array_walk,array_filteror a nestedforeach, 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)).