douhui5529 2016-11-27 23:39 采纳率: 100%
浏览 36

使用laravel检测两个阵列之间的差异

I have two arrays and I need to verify what is different between them, like:
Something exists in array A but not in B,
One argument is diferent in array A when comparing to B...

$temp = import_temp::select('cod_disciplina', 'cod_turma', 'hr_inicio', 'hr_fim', 'dia_semana')->get();
        $turmas;
        foreach($temp as $t)
        {
           $turmas = Horario::select('cod_disciplina', 'cod_turma', 'hr_inicio', 'hr_fim', 'dia_semana')
            ->whereIn('cod_disciplina', $temp->lists('cod_disciplina'))
            ->whereIn('cod_turma', $temp->lists('cod_turma'))
            ->where('ano_semestre', $ano_semestre)->get();
        }

When I do:

 print_r($turmas->toArray()); 
 print_r($temp->toArray());   

I get:
enter image description here

How may I compare these keys and identify when something changed is new or is missing.

Tried working with array_diff_assoc but I get the following error:

Array to string conversion

array_diff_assoc($temp->toArray(),   $turmas->toArray());  

Also tried This that I found in another answer in a similar question but didn't work.

  • 写回答

1条回答 默认 最新

  • doushuhuai7247 2016-11-28 02:01
    关注

    First define your own function to compare arrays:

    function arrayCmp($a, $b) {
        if ($a < $b) {
            return -1;
        } elseif ($a > $b) {
            return 1;
        } else {
            return 0;
        }
    }
    

    Then if you want elements that are present in $a bot not in $b then you call:

    $diff = array_udiff($a, $b, 'arrayCmp');
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能