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

时间:2014-12-13 13:01:21

标签: php multidimensional-array

我试图找到所有活动列表数组和所有已完成活动的列表之间的区别,这些活动将生成尚未完成的所有活动的列表。

这是我的这两个数组的PHP代码:

$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;
}

我使用自定义函数来处理由recursive array_diff()?等几个来源提供的数组递归差异,但它不适用于我的情况。

我还有print_r allMilestone的输出,checkedMilestone&列表:

$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 ) )

你可以指点我出错的地方帮助我,因为人们说自定义功能适合他们。

1 个答案:

答案 0 :(得分:0)

除了可以在一个sql查询中解决这个问题之外,这里有一种使用php内置函数的方法。这比你能写的任何东西都要快得多。

从数据库获取数据时,使用stone_id作为键

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

或类似的东西。我不熟悉php mysql驱动程序的使用,但可能有一个返回关联数组的函数。如果不是像SELECT stone_id, ... FROM ...那样正确地写出你的选择语句并使用:

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

稍后你可以使用array_diff_key()函数来获得......区别!