I want to compare two comma delimited strings in PHP and keep only the values that appear in both -
i have 2 comma delimited strings integers. 1 ids of article types script being called told display (by post) , other ids of article types current logged in user has permission see.
i want generate third comma separated list of integers values appear in both. e.g.:
$want_to_see = "1,5,6,8,10" $current_user_can_see = "1,3,6,10,20" $show = "1,6,10"
you can break 2 strings arrays using explode
, , use array_intersect
keep common values:
$want_to_see = explode(",", "1,5,6,8,10"); $current_user_can_see = explode(",", "1,3,6,10,20") // array of common elements: $show = array_intersect($want_to_see, $current_user_can_see); // if want string: $show_str = implode(",", $show);
Comments
Post a Comment