php怎么去除重复数组值

藏色散人
发布: 2023-03-09 08:22:02
原创
1627 人浏览过

php去除重复数组值的方法:首先打开相应的PHP代码文件;然后通过“array_unique($ordernum);”去掉重复的字符串;最后输出去重后的数组即可。

php怎么去除重复数组值

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php去除数组中重复值,并返回结果!

$data = db('order')->field('ordernum')->where(array('user_id' => $user_id))->select();
登录后复制

查询的数组格式

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'];
}
登录后复制

ordernum数组格式【推荐学习:PHP视频教程

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); //去掉重复的字符串
登录后复制

去重后ordernum 数组格式

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; //拆分后的重组
}
登录后复制

产生的新数组格式

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"
}
登录后复制

以上是php怎么去除重复数组值的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板