dtc9222 2014-12-13 13:01
浏览 44
已采纳

多维数组Diff递归在PHP中不起作用

I am trying to find the difference between an array of a list of all activities and a list of all of the done activities that will yield a list of all activities that is not yet done.

here is my PHP code for these 2 arrays:

$sql="  SELECT * FROM milestone
        WHERE month BETWEEN '$age' - 3 AND '$age' + 3
        ORDER BY month";
$result=mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
    $allMilestone[] = $row;
}

$sql="  SELECT milestone.* FROM `milestone` 
        LEFT JOIN `milestone_transaction` 
        ON milestone.stone_id = milestone_transaction.stone_id 
        WHERE milestone_transaction.registration_no = 1111;";
$result=mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
    $checkedMilestone[] = $row;
}

I am using the custom function for array recursive difference provided by several sources like recursive array_diff()? but it is not working for my case.

I have also print_r the output for allMilestone, checkedMilestone & list:

$allMilestone: Array ( [0] => Array ( [0] => fm1 [stone_id] => fm1 [1] => 3 [month] => 3 [2] => Follow past midline [criteria] => Follow past midline ) [1] => Array ( [0] => fm2 [stone_id] => fm2 [1] => 3 [month] => 3 [2] => Sit look for yarn [criteria] => Sit look for yarn ) [2] => Array ( [0] => gm1 [stone_id] => gm1 [1] => 3 [month] => 3 [2] => Prone lifts head [criteria] => Prone lifts head ) [3] => Array ( [0] => gm2 [stone_id] => gm2 [1] => 3 [month] => 3 [2] => Sit head steady [criteria] => Sit head steady ) [4] => Array ( [0] => hl1 [stone_id] => hl1 [1] => 3 [month] => 3 [2] => Vocalises, not cry [criteria] => Vocalises, not cry ) [5] => Array ( [0] => hl2 [stone_id] => hl2 [1] => 3 [month] => 3 [2] => Laughs [criteria] => Laughs ) [6] => Array ( [0] => ps1 [stone_id] => ps1 [1] => 3 [month] => 3 [2] => Smile responsively [criteria] => Smile responsively ) [7] => Array ( [0] => ps2 [stone_id] => ps2 [1] => 3 [month] => 3 [2] => Resists toy pull [criteria] => Resists toy pull ) ) 

$checkedMilestone: Array ( [0] => Array ( [0] => hl1 [stone_id] => hl1 [1] => 3 [month] => 3 [2] => Vocalises, not cry [criteria] => Vocalises, not cry ) [1] => Array ( [0] => ps1 [stone_id] => ps1 [1] => 3 [month] => 3 [2] => Smile responsively [criteria] => Smile responsively ) )

the difference array: Array ( [0] => Array ( [0] => fm1 [stone_id] => fm1 [2] => Follow past midline [criteria] => Follow past midline ) [1] => Array ( [0] => fm2 [stone_id] => fm2 [2] => Sit look for yarn [criteria] => Sit look for yarn ) [2] => Array ( [0] => gm1 [stone_id] => gm1 [1] => 3 [month] => 3 [2] => Prone lifts head [criteria] => Prone lifts head ) [3] => Array ( [0] => gm2 [stone_id] => gm2 [1] => 3 [month] => 3 [2] => Sit head steady [criteria] => Sit head steady ) [4] => Array ( [0] => hl1 [stone_id] => hl1 [1] => 3 [month] => 3 [2] => Vocalises, not cry [criteria] => Vocalises, not cry ) [5] => Array ( [0] => hl2 [stone_id] => hl2 [1] => 3 [month] => 3 [2] => Laughs [criteria] => Laughs ) [6] => Array ( [0] => ps1 [stone_id] => ps1 [1] => 3 [month] => 3 [2] => Smile responsively [criteria] => Smile responsively ) [7] => Array ( [0] => ps2 [stone_id] => ps2 [1] => 3 [month] => 3 [2] => Resists toy pull [criteria] => Resists toy pull ) )

Can you help me by pointing where I went wrong, as people said that the custom function works properly for them.

  • 写回答

1条回答 默认 最新

  • dtzh131555 2014-12-13 21:58
    关注

    Apart from the fact that this could probably be solved in one sql query, here is a way of using php built in functions. Which are much faster than anything you can write.

    When fetching data from DB use stone_ids as keys

    $allMilestone[$row['stone_id']] = $row;
    // and 
    $checkedMilestone[$row['stone_id']] = $row;
    

    or something like this. I am not fluent in php mysql driver use, but there probably is a function that returns associaltive array. If not than write out your select statements properly like SELECT stone_id, ... FROM ... and use:

    $allMilestone[$row[0]] = $row;
    // and 
    $checkedMilestone[$row[0]] = $row;
    

    Later you can use array_diff_key() function to have ... the difference !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?