Home  >  Article  >  Backend Development  >  PHP three-dimensional array deduplication (sample code)_PHP tutorial

PHP three-dimensional array deduplication (sample code)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:10925browse

Suppose the array is called $my_array;

Copy the code The code is as follows:

// Create a new empty array.
$tmp_array = array();

$new_array = array();

// 1. Loop out all the rows. ($val is a certain row)
foreach($my_array as $k => $val){

$hash = md5(json_encode($val));
if (in_array($hash, $tmp_array)) {
echo('This line already exists');
}else {
// 2. In the body of the foreach loop, assign the hash of each row of array object to the temporary array.
$tmp_array[] = $hash;
$new_array[] = $ val;
}
}

print_r($new_array);


$new_array is the filtered array without duplicate data.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825203.htmlTechArticleAssume the array is called $my_array; copy the code as follows: // Create a new empty array. $tmp_array = array( ); $new_array = array(); // 1. Loop out all the rows. ($val is a certain row)...
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