0

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.

Community
  • 1
  • 1
Kelvin
  • 873
  • 2
  • 9
  • 20

1 Answers1

0

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 !

Konstantin
  • 3,294
  • 21
  • 23
  • I wanted to solve this using one mysql query, but I cannot find the correct logic. But your way allows me to do what I wanna get. Thanks a lot for helping! – Kelvin Dec 14 '14 at 04:25