PHP二维数组切割为字符串并去除重复的值
应用场景在于需要查询出某一个rent_contract_id所有有关的id及rent_contract_parent_id,然后组装成字符串便于后期使用FIND_IN_SET进行查询操作:
- // select rent_contract_id,rent_contract_parent_id from tb_rent_contract where rent_contract_id =797 or rent_contract_parent_id = 797
- $arr = array(array('rent_contract_id'=>555,'rent_contract_parent_id'=>666),array('rent_contract_id'=>777,'rent_contract_parent_id'=>555),array('rent_contract_id'=>888,'rent_contract_parent_id'=>777));
- $str_arr = ""; //空字符串,便于遍历后的追加
- foreach ($arr as $ke => $va) {
- $str_arr .= implode(',', $va).",";
- }
- $arr_str = explode(",",$str_arr); //字符串组装为数组
- $arr_str = array_unique($arr_str);//去除重复的值
- $str = implode(',',$arr_str); //分割为字符串
- var_dump($str); //string(16) "555,666,777,888,"