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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化