Straight to the point...
I have an array ($is_anonymous_ary
) that looks like this:
array (
[80] => 1
[57] => 1
[66] =>
[60] =>
[90] => 1
)
And another array ($user_id_ary
) like this one:
array (
[0] => 80
[1] => 30
[2] => 57
[3] => 89
[4] => 66
[5] => 60
[6] => 90
)
I need to unset values on the $user_id_ary
based on the first array. So, if the value from $is_anonymous_ary
is 1 (true), then take the key from that array, check against $user_id_ary
, and unset the keys from $user_id_ary
which had the value from the keys from $is_anonymous_ary
.
I complicated the description a bit, here is how I need my final result:
user_id_ary = array(
[0] => 30
[1] => 89
[2] => 66
[3] => 60
)
As you see all keys from the $is_anonymous_ary
that had a TRUE
value, are gone in the second array. which had the keys from the first array as values in the second array.
Hope I made myself clear.
Code Inspection: Duplicate array keysReports duplicate keys in array declarations. If multiple elements in the array declaration use the same key, only the last one will be used, and all others will be overwritten.
The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
There is an alternative way to change the key of an array element when working with a full array - without changing the order of the array. It's simply to copy the array into a new array.
pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.
Try array_filter:
$user_id_ary = array_filter($user_id_ary, function($var) use ($is_anonymous_ary) {
return !(isset($is_anonymous_ary[$var]) && $is_anonymous_ary[$var] === 1);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With