如何合并两个数组没有在PHP中的重复值? [英] How to merge two arrays without duplicate values in php?

查看:100
本文介绍了如何合并两个数组没有在PHP中的重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:
1.这里的每个对象是从数据库retrived一行。

 阵列
      1 =>
        对象(stdClass的)[41]
          公众'ID'=>串1(长度= 1)
          公众标识码'=>字符串'25'(长度= 2)
          公众SECTION_ID'=>串'2'(长度= 1)
          公众student_id数据'=>串1(长度= 1)
          公众日期=>字符串'2011-11-26'(长度= 10)
          公众出席'=>字符串'present'(长度= 7)
2 =>
        对象(stdClass的)[41]
          公众'ID'=>串1(长度= 1)
          公众标识码'=>字符串'25'(长度= 2)
          公众SECTION_ID'=>串'2'(长度= 1)
          公众student_id数据'=>字符串'3'(长度= 1)
          公众日期=>字符串'2011-11-26'(长度= 10)
          公众出席'=>字符串'present'(长度= 7)

2。另一个数组是从我的形式,这看起来是这样的。

 阵列
  0 =>
    排列
      标识码'=>字符串'25'(长度= 2)
      SECTION_ID'=>串'2'(长度= 1)
      student_id数据'=> INT 1
      '日期'=>字符串'2011-11-26 00:00:00'(长度= 19)
      '出勤'=>字符串'present'(长度= 7)
  1 =>
    排列
      标识码'=>字符串'25'(长度= 2)
      SECTION_ID'=>串'2'(长度= 1)
      student_id数据'=> INT 2
      '日期'=>字符串'2011-11-26 00:00:00'(长度= 19)
      '出勤'=>字符串'present'(长度= 7)

下面是我想要做的是:结果
- 比较这两个,并检查重点student_id数据和日期已经在数据库或没有结果。
- 从第二阵列中,从表单数据,删除重复的和插入的数据结果。
最后的结果应该是:结果

 阵列
  0 =>
    排列
      标识码'=>字符串'25'(长度= 2)
      SECTION_ID'=>串'2'(长度= 1)
      student_id数据'=> INT 2
      '日期'=>字符串'2011-11-26 00:00:00'(长度= 19)
      '出勤'=>字符串'present'(长度= 7)


解决方案

尝试:


$ C = [array_merge] [1]($ A,$ B);后续代码var_dump([array_unique] [1]($ C));

希望它能帮助

I have two arrays: 1. each object here is a row retrived from database.

    array
      1 => 
        object(stdClass)[41]
          public 'id' => string '1' (length=1)
          public 'class_id' => string '25' (length=2)
          public 'section_id' => string '2' (length=1)
          public 'student_id' => string '1' (length=1)
          public 'date' => string '2011-11-27' (length=10)
          public 'attendance' => string 'present' (length=7)
2 => 
        object(stdClass)[41]
          public 'id' => string '1' (length=1)
          public 'class_id' => string '25' (length=2)
          public 'section_id' => string '2' (length=1)
          public 'student_id' => string '3' (length=1)
          public 'date' => string '2011-11-27' (length=10)
          public 'attendance' => string 'present' (length=7)

2. Another array is from my form and this looks like this.

array
  0 => 
    array
      'class_id' => string '25' (length=2)
      'section_id' => string '2' (length=1)
      'student_id' => int 1
      'date' => string '2011-11-27 00:00:00' (length=19)
      'attendance' => string 'present' (length=7)
  1 => 
    array
      'class_id' => string '25' (length=2)
      'section_id' => string '2' (length=1)
      'student_id' => int 2
      'date' => string '2011-11-27 00:00:00' (length=19)
      'attendance' => string 'present' (length=7)

Here what I want to do is:
- compare these two and check if key student_id and date are already on database or not.
- and from second array which is from form data, remove duplicate and insert into data.
The final result should be:

array
  0 => 
    array
      'class_id' => string '25' (length=2)
      'section_id' => string '2' (length=1)
      'student_id' => int 2
      'date' => string '2011-11-27 00:00:00' (length=19)
      'attendance' => string 'present' (length=7)

解决方案

Try:

$c = [array_merge][1]($a,$b);

var_dump([array_unique][1]($c));

Hope it helps

这篇关于如何合并两个数组没有在PHP中的重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆