How to remove duplicate array values ​​in php

藏色散人
Release: 2023-03-09 08:22:02
Original
1555 people have browsed it

php method to remove duplicate array values: first open the corresponding PHP code file; then remove duplicate strings through "array_unique($ordernum);"; finally output the deduplicated array.

How to remove duplicate array values ​​in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php removes duplicate values ​​​​in the array and returns the result!

$data = db('order')->field('ordernum')->where(array('user_id' => $user_id))->select();
Copy after login

Query array format

array(16) {
  [0] => array(1) {
    ["ordernum"] => string(18) "190415165656156213"
  }
  [1] => array(1) {
    ["ordernum"] => string(18) "190415165656156213"
  }
  [2] => array(1) {
    ["ordernum"] => string(18) "190415165656156213"
  }
  [3] => array(1) {
    ["ordernum"] => string(18) "190415170056183687"
  }
  [4] => array(1) {
    ["ordernum"] => string(18) "190415170056183687"
  }
  [5] => array(1) {
    ["ordernum"] => string(18) "190415170056183687"
  }
  [6] => array(1) {
    ["ordernum"] => string(18) "190415170345120966"
  }
  [7] => array(1) {
    ["ordernum"] => string(18) "190415170345120966"
  }
  [8] => array(1) {
    ["ordernum"] => string(18) "190415170345120966"
  }
  [9] => array(1) {
    ["ordernum"] => string(18) "190416174929158053"
  }
  [10] => array(1) {
    ["ordernum"] => string(18) "190416174929158053"
  }
  [11] => array(1) {
    ["ordernum"] => string(18) "190416194457158437"
  }
  [12] => array(1) {
    ["ordernum"] => string(18) "190416194457158437"
  }
  [13] => array(1) {
    ["ordernum"] => string(18) "190416195027154990"
  }
  [14] => array(1) {
    ["ordernum"] => string(18) "190416195027154990"
  }
  [15] => array(1) {
    ["ordernum"] => string(18) "190417124518178758"
  }
}
foreach ($data as $k => $v) {
     $ordernum[] = $v['ordernum'];
}
Copy after login

ordernum array format [Recommended learning: PHP video tutorial]

array(16) {
  [0] => string(18) "190415165656156213"
  [1] => string(18) "190415165656156213"
  [2] => string(18) "190415165656156213"
  [3] => string(18) "190415170056183687"
  [4] => string(18) "190415170056183687"
  [5] => string(18) "190415170056183687"
  [6] => string(18) "190415170345120966"
  [7] => string(18) "190415170345120966"
  [8] => string(18) "190415170345120966"
  [9] => string(18) "190416174929158053"
  [10] => string(18) "190416174929158053"
  [11] => string(18) "190416194457158437"
  [12] => string(18) "190416194457158437"
  [13] => string(18) "190416195027154990"
  [14] => string(18) "190416195027154990"
  [15] => string(18) "190417124518178758"
}
$ordernum = array_unique($ordernum); //去掉重复的字符串
Copy after login

ordernum array format after deduplication

array(7) {
  [0] => string(18) "190415165656156213"
  [3] => string(18) "190415170056183687"
  [6] => string(18) "190415170345120966"
  [9] => string(18) "190416174929158053"
  [11] => string(18) "190416194457158437"
  [13] => string(18) "190416195027154990"
  [15] => string(18) "190417124518178758"
}
foreach ($ordernum as $k => $v) {
      $new_ordernum[] = $v; //拆分后的重组
}
Copy after login

The new array format generated

array(7) {
  [0] => string(18) "190415165656156213"
  [1] => string(18) "190415170056183687"
  [2] => string(18) "190415170345120966"
  [3] => string(18) "190416174929158053"
  [4] => string(18) "190416194457158437"
  [5] => string(18) "190416195027154990"
  [6] => string(18) "190417124518178758"
}
Copy after login

The above is the detailed content of How to remove duplicate array values ​​in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!