多维数组差异递归不能在PHP中工作
问题描述:
我正在尝试查找所有活动列表的数组与所有已完成活动的列表之间的差异,这些活动将生成尚未完成的所有活动的列表。多维数组差异递归不能在PHP中工作
这里是我的这两个数组的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))
你能帮我通过指出我哪里错了,因为人们说,自定义功能的正常使用他们。
答
除了这可能可以在一个SQL查询中解决的事实,这里是一种使用内置函数的php的方法。这比你能写的任何东西都要快得多。
当从数据库获取数据使用stone_id
S作为密钥
$allMilestone[$row['stone_id']] = $row;
// and
$checkedMilestone[$row['stone_id']] = $row;
或这样的事情。我不太熟悉php mysql驱动程序的使用,但可能有一个返回associaltive数组的函数。如果不是正确地写出你的SELECT语句像SELECT stone_id, ... FROM ...
及用途:
$allMilestone[$row[0]] = $row;
// and
$checkedMilestone[$row[0]] = $row;
稍后,可以使用array_diff_key()函数有...的区别!
我想解决这个使用一个MySQL查询,但我找不到正确的逻辑。但你的方式可以让我做我想做的事。非常感谢您的帮助! – 2014-12-14 04:25:08