我有两个逗号分隔的整数字符串。一个是被告知要显示的文章类型的ID(通过POST),另一个是当前登录用户实际有权查看的文章类型的ID。
我想生成第三个逗号分隔的整数列表,其中包含两个值。 e.g:
$want_to_see = "1,5,6,8,10"
$current_user_can_see = "1,3,6,10,20"
$show = "1,6,10"
答案 0 :(得分:2)
您可以使用explode
将两个字符串分解为数组,然后使用array_intersect
仅保留常用值:
$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 you want it as a string:
$show_str = implode(",", $show);