Home  >  Article  >  Backend Development  >  PHP cut two-dimensional array into string and remove duplicate values

PHP cut two-dimensional array into string and remove duplicate values

不言
不言Original
2018-04-13 14:34:461638browse

The content of this article is about the code for cutting PHP two-dimensional array into strings and removing duplicate values. Now I share it with you. Friends in need can refer to it.

The application scenario lies in the need to query All relevant ids and rent_contract_parent_id of a certain rent_contract_id are then assembled into a string for later query operation using 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,"


The above is the detailed content of PHP cut two-dimensional array into string and remove duplicate values. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn