drap5081683 2014-04-24 17:42
浏览 43
已采纳

在匹配时将不同的阵列单元组合到一个阵列

I can not wrap my head around how to do the following. I have two different lenght and different dimensional arrays that I want to combine so that the result array will have arrays1 + array2 cell that did not match.

This should help you understand what I mean

$dbquery_results1[0][] = array("1","5","b99");
$dbquery_results1[1][] = array("4","12","www");
$dbquery_results1[2][] = array("10","32","ccc");
$dbquery_results1[3][] = array("7","142","xx");

$dbquery_results2[0][] = array("c","10");
$dbquery_results2[1][] = array("as","1");
$dbquery_results2[2][] = array("fe","7");

$dbquery_combination[0][] = array("1","5","b99","as");
$dbquery_combination[1][] = array("7","142","xx","fe");

What I would like to get done

      $i=0;
 while(!empty($dbquery_results1[$i][0])) {
        If($dbquery_results1[$i][0] == $dbquery_results2[any of the dimensions here][2])  
          {
              $dbquery_combination[] = $dbquery_results1[$i][]+ $dbquery_results2[x-dimension][2];
         }  $i++;
            }

So only if array1 has at [][0] same value as array2 [any dimension][1] will array1 + the array2 cell that is different be saved at array3.

Sorry that I am not too good expressing myself. Thanks a lot in advance.

  • 写回答

2条回答 默认 最新

  • douzhong1730 2014-04-24 18:58
    关注

    So if you don't have the choice you can do it like this :

    <?php
    $dbquery_results1[0] = array("1","5","b99");
    $dbquery_results1[1] = array("4","12","www");
    $dbquery_results1[2] = array("10","32","ccc");
    $dbquery_results1[3] = array("7","142","xx");
    
    $dbquery_results2[0] = array("c","10");
    $dbquery_results2[1] = array("as","1");
    $dbquery_results2[2] = array("fe","7");
    
    foreach($dbquery_results1 as $line_r1)
    {
        foreach($dbquery_results2 as $line_r2)
        {
            if($line_r1[0] == $line_r2[1])
            {
                $line = $line_r1;
                $line[] = $line_r2[0];
    
                $dbquery_combination[] = $line;
            }
        }
    }
    
    print_r($dbquery_combination);
    ?>
    

    And the result is :

    Array (
            [0] => Array ( [0] => 1 [1] => 5 [2] => b99 [3] => as )
            [1] => Array ( [0] => 10 [1] => 32 [2] => ccc [3] => c )
            [2] => Array ( [0] => 7 [1] => 142 [2] => xx [3] => fe )
          )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致