PHP 用于比较数组,$_POST 数据和 $_SESSION 数组

PHP for comparing arrays, $_POST data and $_SESSION array

所以我已经停留了几个小时试图比较存储为变量的 $_SESSION 数据与来自更新表单字段的 $_POST 数据但是当我使用数组 array_diff_assoc 或 array_diff 我获取整个会话数据数组或整个 post 数据数组(取决于我如何在数组函数中对它们进行排序),而不是区别。我希望能够只输出差异。

<?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);
}

?>

注意:当我 print_r $_SESSION['your_data'] 或 $_POST 时,我可以看到数组。目标是使用 phpmail()

发送包含表单更新字段的电子邮件

我认为两个数组中都没有匹配的键和值! 这就是为什么您要在 $result 变量中返回整个数组。

我已经在下面解释了为什么会这样:

array_diff_assoc

array_diff_assoc 将第一个数组中的 keysvalues 与第二个数组进行比较。 第一个数组中存在但第二个数组中不存在的键和值返回到$result

例如

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

输出:

Array ( ) 

如果你现在比较 $array2 和 $array1

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

输出:

Array ( [d] => salad )

如您所见,唯一的 non-matching 键和值是 "d" => "salad"

现在让我们试试这个:

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

输出:

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

如您所见,两个数组中的值相同但键不同,因此 $array1 返回到 $result。因此,如果您只想比较 .

,则必须使用 array_diff()

array_diff

array_diff 只比较数组中的值。现在让我们再次尝试上面的相同示例!

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

输出:

Array ( ) 

这里键不同但值相同所以所有结果都匹配,所以返回空数组!

你的问题的结论

由于您尝试了 array_diff() 和 array_diff_assoc(),您的两个数组既没有相同的值,也没有相同的键和值,因此第一个参数中的整个数组返回两种情况!

我觉得你得用别的方法比较一下!

[编辑:您可以使用 array_diff() 比较 $row 和 $_POST,如问题评论中所述] 参考文献:

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

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

  • 我花了一些时间 re-write 你的代码尽我所能,但没有与你相同的数据库信息。

  • 如果 works/errors 告诉我! :)

关键变化:

  1. 将 'MYSQLI_ASSOC' 添加到 'mysqli_fetch_array'
  2. 已更改:
'$your_data[] = $user_info;' 

'_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>';
}

在研究了社区成员提供的一些解决方案后,我编辑了我的代码并且成功了。我的代码现在看起来像这样;

<?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, MYSQLI_ASSOC)) {
                                $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['address_line_2'] =  $row['address_line_2'];
                                 $user_info['town_city'] =  $row['town_city'];
                                 $user_info['county'] =  $row['county'];
                                 $user_info['postcode'] =  $row['postcode'];
                               //  $your_data[] = $user_info;
                                $_SESSION['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($_POST, $_SESSION['your_data']);
  // '<pre></pre>' formats array output for html
  echo '<pre>';
  print_r($result);
  echo '</pre>';
}
?>

注意:$result = array_diff_assoc($_POST, $_SESSION['your_data'])。顺序颠倒了,求差价