Home > Software design >  PHP for comparing arrays, $_POST data and $_SESSION array
PHP for comparing arrays, $_POST data and $_SESSION array

Time:05-29

So I've been stuck for a few hours trying to compare $_SESSION data stored as a variable with $_POST data from updated form fields but when I use array array_diff_assoc or array_diff i get either the whole session data array or the whole post data array(depending oh how I order them in the array function), and not the difference. I want to be able to output the difference only.

<?php
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
$query = "SELECT * FROM updatetest WHERE username = '{$username}' ";       
$select_users= mysqli_query($db, $query);
 while ($row = mysqli_fetch_array($select_users)) {
             $user_info['name'] =  $row['name'];
             $user_info['phone'] =  $row['phone'];
             $user_info['dob'] =  $row['dob'];
             $user_info['email'] =  $row['email'];
             $user_info['address_line_1'] = $row['address_line_1'];
             $user_info['town_city'] =  $row['town_city'];
             $user_info['postcode'] =  $row['postcode'];
             $your_data[]=$user_info;
               }
}
?> 

 <?php
 if(isset($_POST['submit'])) {

         $name= $_POST['name'];
         $phone = $_POST['phone'];
         $dob = $_POST['dob'];
         $email = $_POST['email'];
         $address_line_1 = $_POST['address_line_1'];
         $town_city = $_POST['town_city'];
         $postcode = $_POST['postcode'];

$result = array_diff_assoc($_SESSION['your_data'],$_POST);
 print_r($result);
}

?>

Note: when I print_r either $_SESSION['your_data'] or $_POST, I can see the arrays. The goal is to send an email containing the form's updated fields using phpmail()

CodePudding user response:

I think there are no matching keys and values in both the arrays! That is why you are getting the whole array returned in the $result variable.

I have explained why this happens below:

array_diff_assoc

array_diff_assoc compares both the keys and values in the first array to the second array. The keys and values which are present in the first array but not in the second array is returned to the $result

For example

<?php

$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("a" => "apple",  "b" => "banana", "c" => "mango");
$result = array_diff_assoc($array1, $array2);
print_r($result)
?>

Output:

Array ( ) 

If you now compare $array2 with $array1

<?php
$result = array_diff_assoc($array2, $array1);
print_r($result);
?>

Output:

Array ( [d] => salad )

As you can see the only non-matching key and value is "d" => "salad"

Now let's try this:

<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("m" => "apple",  "n" => "banana", "o" => "mango");
$result = array_diff_assoc($array1, $array2);
print_r($result)
?>

Output:

Array ( [a] => apple [b] => banana [c] => mango ) 

As you can see the values are same in both the arrays but the keys are different, so the $array1 gets returned to the $result. So you have to use array_diff(), if you want to compare the values only.

array_diff

array_diff compares only the values in the array. Now lets try the above same example again!

<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("m" => "apple",  "n" => "banana", "o" => "mango");
$result = array_diff($array1, $array2);
print_r($result)
?>

Output:

Array ( ) 

Here the keys are different but the values are same so all results are matching, so empty array is returned!

Conclusion to your problem

Since you tried both array_diff() and array_diff_assoc(), both of your arrays neither have same values nor both same key and value therefore the whole array in the first argument gets returned in both the cases!

I think you have to use some other method to compare them!

[Edit: You could compare the the $row and $_POST using array_diff() as mentioned under the comment of the question] References:

https://www.php.net/manual/en/function.array-diff-assoc.php

https://www.php.net/manual/en/function.array-diff.php

CodePudding user response:

  • i took some time to re-write your code to the best of my ability without having the same DB information as you.

  • let me know if it works/errors! :)

key changes:

  1. added 'MYSQLI_ASSOC' to 'mysqli_fetch_array'
  2. changed:
'$your_data[] = $user_info;' 

to

'_SESSION['your_data'] = $user_info;'
$_SESSION['username'] = 'userone';

if (!empty($_SESSION['username'])) {

  // $_SESSION variables are safely stored server side
  //$username = $_SESSION['username'];
  $select = '*';
  $table = 'updatetest';
  $column = $_SESSION['username'];

  $query = 'SELECT ' . $select . ' FROM ' . $table . ' WHERE username = ' . $column;
  //$query = "SELECT * FROM updatetest WHERE username = '{$username}' ";

  $select_users = mysqli_query($db, $query);

  /* associative array 
  * https://www.php.net/manual/en/mysqli-result.fetch-array.php
  */
  while ($row = mysqli_fetch_array($select_users, MYSQLI_ASSOC)) {
    //while ($row = mysqli_fetch_array($select_users)) {

    $user_info['name'] =  $row['name'];
    $user_info['phone'] =  $row['phone'];
    $user_info['dob'] =  $row['dob'];
    $user_info['email'] =  $row['email'];
    $user_info['address_line_1'] = $row['address_line_1'];
    $user_info['town_city'] =  $row['town_city'];
    $user_info['postcode'] =  $row['postcode'];
    //$your_data[] = $user_info;
    $_SESSION['your_data'] = $user_info;
  }
}

// form submission
if (isset($_POST['submit'])) {

  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $dob = $_POST['dob'];
  $email = $_POST['email'];
  $address_line_1 = $_POST['address_line_1'];
  $town_city = $_POST['town_city'];
  $postcode = $_POST['postcode'];

  $result = array_diff_assoc($_SESSION['your_data'], $_POST);
  // '<pre></pre>' formats array output for html
  echo '<pre>';
  print_r($result);
  echo '</pre>';
}
  • Related