简体   繁体   English

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

[英]PHP for comparing arrays, $_POST data and $_SESSION array

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.因此,我被困了几个小时,试图将存储为变量的 $_SESSION 数据与更新的表单字段中的 $_POST 数据进行比较,但是当我使用数组 array_diff_assoc 或 array_diff 时,我得到了整个会话数据数组或整个 post 数据数组(取决于哦我如何在数组函数中对它们进行排序),而不是区别。 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.注意:当我打印 $_SESSION['your_data'] 或 $_POST 时,我可以看到数组。 The goal is to send an email containing the form's updated fields using phpmail()目标是使用 phpmail() 发送包含表单更新字段的电子邮件

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.这就是为什么您在 $result 变量中返回整个数组的原因。

I have explained why this happens below:我在下面解释了为什么会发生这种情况:

array_diff_assoc array_diff_assoc

array_diff_assoc compares both the keys and values in the first array to the second array. array_diff_assoc将第一个数组中的与第二个数组进行比较。 The keys and values which are present in the first array but not in the second array is returned to the $result一个数组中存在但第二个数组中没有的键和值返回到$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如果您现在将 $array2 与 $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"如您所见,唯一不匹配的键和值是"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.如您所见,两个数组中的值相同,但键不同,因此 $array1 返回到 $result。 So you have to use array_diff() , if you want to compare the values only .因此,如果您只想比较,则必须使用array_diff()

array_diff数组差异

array_diff compares only the values in the array. array_diff仅比较数组中的值。 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!由于您尝试了 array_diff() 和 array_diff_assoc(),因此您的两个数组既没有相同的值,也没有相同的键和值,因此在这两种情况下都会返回第一个参数中的整个数组!

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: [编辑:您可以使用问题评论下提到的 array_diff() 比较 $row 和 $_POST] 参考:

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

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

  • 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'将 'MYSQLI_ASSOC' 添加到 '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>';
}

After going through some solutions provided by community members, I edited my code and it worked.在浏览了社区成员提供的一些解决方案之后,我编辑了我的代码并且它工作了。 My code now looks like this;我的代码现在看起来像这样;

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

Note: $result = array_diff_assoc($_POST, $_SESSION['your_data']).注意:$result = array_diff_assoc($_POST, $_SESSION['your_data'])。 The order was reversed, to get the difference.顺序颠倒,以获得差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM