用于比较数组,$ _ post数据和$ _Session Array的PHP
因此,我已经卡住了几个小时,试图将存储为变量的$ _session数据与来自更新的表单字段中的$ _post数据存储在一起,但是当我使用array array_diff_assoc或array_diff时(取决于我如何在数组函数中订购它们),而不是区别。我希望能够仅输出差异。
<?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()发送包含表格更新字段的电子邮件
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我花了一些时间以尽我所能重写您的代码,而没有与您相同的DB信息。
让我知道它是否有效/错误! :)
密钥更改:
中
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:
to
经过社区成员提供的一些解决方案后,我编辑了我的代码并奏效了。我的代码现在看起来像这样;
注意:$ result = array_diff_assoc($ _ post,$ _session ['your_data'])。订单被颠倒了,以获取差异。
After going through some solutions provided by community members, I edited my code and it worked. My code now looks like this;
Note: $result = array_diff_assoc($_POST, $_SESSION['your_data']). The order was reversed, to get the difference.
我认为两个阵列中都没有匹配的键和值!
这就是为什么您将整个数组返回到$ result变量中的原因。
我已经解释了为什么下面发生这种情况的原因:
array_diff_assoc
array_diff_assoc
在第一个数组中将键和 values 比较了第二个数组。 第一个数组中存在的键和值,但不在第二个数组中返回$ $ result
例如,
输出:
如果您现在,将$ array2与$ array1
输出进行比较:
array([d] =&gt; salad)
,您可以看到唯一的非匹配键,值为
“ d” d“ =&gt; “沙拉”
现在让我们尝试一下:
输出:
您可以看到两个数组中的值相同,但是键不同,因此$ array1被返回到$ result。因此,如果要仅比较值,则必须使用
array_diff()
。array_diff
array_diff
仅比较数组中的值。现在让我们再次尝试以上示例!输出:
这里的键不同,但值相同,因此所有结果都是匹配的,因此返回空数组!
结论您的问题,
因为您同时尝试了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
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
Output:
If you now compare $array2 with $array1
Output:
Array ( [d] => salad )
As you can see the only non-matching key and value is
"d" => "salad"
Now let's try this:
Output:
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!Output:
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