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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置