In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will show you how php removes duplicate data. The knowledge points in the article are introduced in great detail. Friends who think it is helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "how to remove duplicate data from php".
Php removes the duplicate data: 1, remove the duplicate data in the one-dimensional array through the array_unique function; 2, remove the duplicate data in the multi-dimensional array through the custom unique_arr method.
This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer
How does php remove duplicate data?
PHP removes duplicate array data
There is a built-in function array_unique () for removing duplicates in PHP array, but the array_unique function of php is only suitable for one-dimensional array, but not for multi-dimensional array. Here is an array_unique function of two-dimensional array.
Function unique_arr ($array2D,$stkeep=false,$ndformat=true) {/ / determine whether to retain the first-order array key (the first-order array key can be non-numeric) if ($stkeep) $stArr = array_keys ($array2D); / / determine whether to retain the secondary array key (all secondary array keys must be the same) if ($ndformat) $ndArr = array_keys (end ($array2D)) / / Dimension reduction, or you can use implode to convert an one-dimensional array to a comma concatenated string foreach ($array2D as $v) {$v = join (",", $v); $temp [] = $v;} / / remove the duplicate string, that is, the repeating one-dimensional array $temp = array_unique ($temp) / then reassemble the disassembled array foreach ($temp as $k = > $v) {if ($stkeep) $k = $stArr [$k]; if ($ndformat) {$tempArr = explode (",", $v); foreach ($tempArr as $ndkey = > $ndval) $output [$k] [$ndArr [$ndkey]] = $ndval;} else $output [$k] = explode (",", $v);} return $output }
Demo:
$array2D = array ('first'= > array (' title'= > '1111),' second'= > array ('title'= >' 1111), 'third'= > array (' title'= >'22')); print_r ($array2D); print_r (unique_arr ($array2D,true))
Method 2:
Example 1
First, the PHP one-dimensional array removes duplicates (using the array_unique function).
Example 1, code:
Output result:
Array ([0] = > apple [1] = > banana [2] = > pear [4] = > wail [5] = > watermalon)
Second, duplicates of the PHP two-dimensional array:
For the two-dimensional array, we discuss it in two cases, one is because the value of a key name cannot be duplicated, delete the duplicate item.
The other removes duplicates because the internal one-dimensional array cannot be exactly the same.
Example 1, because the value of a key name cannot be duplicated, delete the duplicate item.
Code:
Output result:
Array ([0] = > Array ([id] = > 123 [name] = > Zhang San) [1] = > Array ([id] = > 124 [name] = > Wang Wu) [2] = > Array ([id] = > 125 [name] = > Zhao Liu) [3] = > Array ([id] = > 126 [name] = > Zhao Liu))
In example 2, duplicates are deleted because the internal one-dimensional array cannot be exactly the same.
Code:
Array ([0] = > Array ([0] = > 123 [1] = > Zhang San) [1] = > Array ([0] = > 123 [1] = > Li Si) [2] = > Array ([0] = > 124 [1] = > Wang Wu) [4] = > Array ([0] = > 126 [1] = > Zhao Liu)) what are the php frameworks of php: 1. Laravel,Laravel is a free and open source PHP application framework. 2. Phalcon,Phalcon is the fastest PHP framework. 3. Symfony,Symfony is a PHP framework for Web projects. 4. Yii,Yii is a fast, secure and professional PHP framework. CodeIgniter,CodeIgniter is a very agile open source PHP framework. 6. CakePHP,CakePHP is an old PHP framework. 7. Kohana recorder Kohana is an agile but powerful PHP framework.
Thank you for your reading, the above is the whole content of "how to remove duplicate data from php". Friends who learn to learn to do it quickly. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.