duanmei4149 2016-04-29 07:03
浏览 69
已采纳

php多维数组键值从两个不同的数组中搜索

I have two different array. The array are like

first array is like

Array
(
    [0] => Array
        (
            [source_language] => English
            [target_language] => Array
                (
                    [0] => German
                    [1] => Norwegian
                )

            [title] => file 1.xlsx
            [file_name] => 1461911750_file_test.xlsx
        )

    [1] => Array
        (
            [source_language] => Hindi
            [target_language] => Array
                (
                    [0] => Belarusian
                )

            [title] => new files.xlsx
            [file_name] => 1461912206_file_here_files.xlsx
        )

)

Second array is like

Array
(
    [1] => Array
        (
            [source_language] => English
            [target_language] => Array
                (
                    [0] => Urdu
                    [1] => Hindi
                )

        )

    [2] => Array
        (
            [source_language] => Hindi
            [target_language] => Array
                (
                    [0] => Norwegian
                )

        )
    [3] => Array
        (
            [source_language] => Hindi
            [target_language] => Array
                (
                    [0] => German
                )

        )
    [3] => Array
        (
            [source_language] => English
            [target_language] => Array
                (
                    [0] => German
                    [1] => Norwegian
                )

        )                

)

Now you can both array same key for source_langauge and target_language . The title and file_name key is available only in first array

So I want that the first array will search all the source_language and target_language from the second array and show both matching arrays and non matching arrays

So the output should be like this

The matching array is

[0] => Array
    (
        [source_language] => English
        [target_language] => Array
            (
                [0] => German
                [1] => Norwegian
            )

        [title] => file 1.xlsx
        [file_name] => 1461911750_file_test.xlsx
    )

as it has same values source language and target language in second array

The other result will show is

non matching array is

[0] => Array
    (
        [source_language] => Hindi
        [target_language] => Array
            (
                [0] => Belarusian
            )

        [title] => new files.xlsx
        [file_name] => 1461912206_file_here_files.xlsx
    )

as this source language and target languages are not found in second array.

Update

I have tried so far is

$diffs = [];

        foreach ($first_array as $a1) {
          $h1 = md5(json_encode($a1));
          $found = false;

          foreach ($second_array as $a2) {
            if (md5(json_encode($a2)) == $h1) {
              $found = true;
              break;
            }
          }


          if ( !$found ) {
            $diffs []= $a1;
          }
        }

But this one is not working at all

  • 写回答

3条回答 默认 最新

  • dou760663 2016-04-29 07:31
    关注

    First off, you can't compare $h1 to $a2 since they don't share the exact same structure.

    $h1 still has that title and filename key pair values.

    No need to use md5 and json_encode, since you just need to compare two string values:

    $match = $no_match = array();
    foreach($first as $f) {
        $found = false;
        foreach($second as $s) {
            if(
                $f['source_language'] === $s['source_language'] &&
                $f['target_language'] === $s['target_language']
                ) {
                $match[] = $f;
                $found = true;
            }
        }
        if(!$found) {
            $no_match[] = $f;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料