Php 更改我的数组,以便将值存储在一起
我有多个可能的用户通过AJAX从表单发布到我的PHP脚本 在该脚本中,我有以下代码:Php 更改我的数组,以便将值存储在一起,php,arrays,Php,Arrays,我有多个可能的用户通过AJAX从表单发布到我的PHP脚本 在该脚本中,我有以下代码: parse_str($_POST['users'], $useroutput); foreach($useroutput as $key => $user){ // If both fields are filled in: if(!empty($user['username']) OR !empty($user['password'])){ $userstring .= $user['
parse_str($_POST['users'], $useroutput);
foreach($useroutput as $key => $user){
// If both fields are filled in:
if(!empty($user['username']) OR !empty($user['password'])){
$userstring .= $user['username'].' '.$user['password'].'<br>';
}else{
echo 'Empty';
}
}
如何将数组更改为如下所示:
Array(
[0] => Array
(
[username] = myusername
[password] = mypassword
)
[1] => Array
(
[username] = anotherusername
[password] = anotherpassword
)
)
我尝试了不同的方法来发布数据,但直到现在,没有任何方法能够提供我所需要的数组
以下是我将表单数据发布到PHP脚本的方式:
// Add/edit users script
$( "#companywrap" ).on("click", "#saveuser", function( event ) {
// Stop normal form behaviour
event.preventDefault();
var $form = $("#userform"),
postBody = $form.serialize(),
url = $form.attr( "action" );
var posting = $.post( url, {users: postBody});
// Show result in a div
posting.done(function( data ) {
$( ".resultmessageuser" ).empty().slideDown('fast').append( data );
});
});
我的表单标记(按要求):
<form id="userform" action="includes/userscript.php" method="post" enctype="multipart/form-data">
<div class="card m-b-20">
<div class="card-body">
<div class="form-group fieldGroup">
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
<div class="col-sm-8">
<input class="form-control" name="username[]" value="<?PHP echo $getcompany['username']; ?>" type="text" required>
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
<div class="col-sm-8">
<input class="form-control" name="password[]" placeholder="<?PHP echo $editpass; ?>" value='' type="text" required>
</div>
</div>
<div class="input-group-addon">
<a href="javascript:void(0)" class="btn btn-success addMore"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Extra gebruiker</a>
</div>
</div>
<!-- copy of input fields group -->
<div class="form-group fieldGroupCopy" style="display: none;">
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
<div class="col-sm-8">
<input class="form-control" name="username[]" value="" type="text" id="example-text-input">
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
<div class="col-sm-8">
<input class="form-control" name="password[]" placeholder="<?PHP echo $editpass; ?>" value='' type="text" id="example-text-input">
</div>
</div>
<div class="input-group-addon">
<a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Verwijder velden</a>
</div>
</div>
</div>
</div>
</form>
格布鲁伊克斯纳姆酒店
你的循环不正确
当您循环foreach($useroutput as$key=>$user){
您将获得以下信息:
(
[0] => username
[1] => anotherusername
)
此时您没有任何$user['username']
您有$user[0]
和[1]
如果您喜欢这样做:
foreach($useroutput['username'] as $key => $val){
$users[] = array_combine(['username','password'],array_column($useroutput, $key));
}
Var_dump($users);
[
"username" => [
0 => "john"
1 => "jane"
]
"password" => [
0 => "pw1"
1 => "pw2"
]
]
<form>
<input type="text" name="credentials[0][username]">
<input type="text" name="credentials[0][password]">
<input type="text" name="credentials[1][password]">
<input type="text" name="credentials[1][password]">
<button type="submit">Submit</button>
</form>
[
"credentials" => [
0 => [
"username" => "john"
"password" => "pw1"
]
1 => [
"username" => "jane"
"password" => "pw2"
]
]
]
print_r($_POST['users'];
array(
[0] => array(
'username' => 'some_username',
'password' => 'some_password'
)
);
$users数组将在数组中工作,并按预期对用户进行分组。您的循环不正确
当您循环foreach($useroutput as$key=>$user){
您将获得以下信息:
(
[0] => username
[1] => anotherusername
)
此时您没有任何$user['username']
您有$user[0]
和[1]
如果您喜欢这样做:
foreach($useroutput['username'] as $key => $val){
$users[] = array_combine(['username','password'],array_column($useroutput, $key));
}
Var_dump($users);
[
"username" => [
0 => "john"
1 => "jane"
]
"password" => [
0 => "pw1"
1 => "pw2"
]
]
<form>
<input type="text" name="credentials[0][username]">
<input type="text" name="credentials[0][password]">
<input type="text" name="credentials[1][password]">
<input type="text" name="credentials[1][password]">
<button type="submit">Submit</button>
</form>
[
"credentials" => [
0 => [
"username" => "john"
"password" => "pw1"
]
1 => [
"username" => "jane"
"password" => "pw2"
]
]
]
print_r($_POST['users'];
array(
[0] => array(
'username' => 'some_username',
'password' => 'some_password'
)
);
您的$users数组将在数组中工作,并按预期对用户进行分组。如果两个索引都满足要求,您可以尝试:
$new_array = [];
for($i = 0; $i < count($useroutput); $i++){
$new_array[] = ['username'=>$useroutput['username'][$i], 'password'=>$useroutput['password'][$i]];
}
如果两个索引都满足要求,则可以尝试:
$new_array = [];
for($i = 0; $i < count($useroutput); $i++){
$new_array[] = ['username'=>$useroutput['username'][$i], 'password'=>$useroutput['password'][$i]];
}
我认为您应该从修改HTML的呈现方式开始。目前您的表单
呈现方式如下:
<form>
<input type="text" name="username[]">
<input type="text" name="password[]">
<input type="text" name="username[]">
<input type="text" name="password[]">
<button type="submit">Submit</button>
</form>
但是,如果将HTML更改为如下格式:
foreach($useroutput['username'] as $key => $val){
$users[] = array_combine(['username','password'],array_column($useroutput, $key));
}
Var_dump($users);
[
"username" => [
0 => "john"
1 => "jane"
]
"password" => [
0 => "pw1"
1 => "pw2"
]
]
<form>
<input type="text" name="credentials[0][username]">
<input type="text" name="credentials[0][password]">
<input type="text" name="credentials[1][password]">
<input type="text" name="credentials[1][password]">
<button type="submit">Submit</button>
</form>
[
"credentials" => [
0 => [
"username" => "john"
"password" => "pw1"
]
1 => [
"username" => "jane"
"password" => "pw2"
]
]
]
print_r($_POST['users'];
array(
[0] => array(
'username' => 'some_username',
'password' => 'some_password'
)
);
一旦您的HTML被修复,您只需要做一些小的调整:
将$.post(url,{users:postBody});
更改为$.post(url,postBody);
现在,您可以使用最初想要的PHP迭代,如下所示:
foreach($_POST['credentials'] as $credential){
if(!empty($credential['username']) || !empty($credential['password'])){
$userstring .= $credential['username'].' '.$credential['password'].'<br>';
}else{
echo 'Empty';
}
}
foreach($\u POST['credentials']作为$credential){
如果(!empty($credential['username'])| |!empty($credential['password'])){
$userstring.=$credential['username'].'.$credential['password'.'。
;
}否则{
回声“空”;
}
}
我认为您应该从修改HTML的呈现方式开始。目前您的表单
呈现方式如下:
<form>
<input type="text" name="username[]">
<input type="text" name="password[]">
<input type="text" name="username[]">
<input type="text" name="password[]">
<button type="submit">Submit</button>
</form>
但是,如果将HTML更改为如下格式:
foreach($useroutput['username'] as $key => $val){
$users[] = array_combine(['username','password'],array_column($useroutput, $key));
}
Var_dump($users);
[
"username" => [
0 => "john"
1 => "jane"
]
"password" => [
0 => "pw1"
1 => "pw2"
]
]
<form>
<input type="text" name="credentials[0][username]">
<input type="text" name="credentials[0][password]">
<input type="text" name="credentials[1][password]">
<input type="text" name="credentials[1][password]">
<button type="submit">Submit</button>
</form>
[
"credentials" => [
0 => [
"username" => "john"
"password" => "pw1"
]
1 => [
"username" => "jane"
"password" => "pw2"
]
]
]
print_r($_POST['users'];
array(
[0] => array(
'username' => 'some_username',
'password' => 'some_password'
)
);
一旦您的HTML被修复,您只需要做一些小的调整:
将$.post(url,{users:postBody});
更改为$.post(url,postBody);
现在,您可以使用最初想要的PHP迭代,如下所示:
foreach($_POST['credentials'] as $credential){
if(!empty($credential['username']) || !empty($credential['password'])){
$userstring .= $credential['username'].' '.$credential['password'].'<br>';
}else{
echo 'Empty';
}
}
foreach($\u POST['credentials']作为$credential){
如果(!empty($credential['username'])| |!empty($credential['password'])){
$userstring.=$credential['username'].'.$credential['password'.'。
;
}否则{
回声“空”;
}
}
你们想得太多了。输入不好,输出不好。你们没有在html中命名键,这会导致一个枚举数组而不是关联数组。你们不需要做复杂的php循环技巧来解释布局不好的表单标记,你们只需要纠正表单标记,使其按你们想要的方式嵌套
这:
不要太聪明。保持简单。你们这些家伙想得太多了。输入不好,输出不好。你没有在html中命名键,这会导致枚举数组而不是关联数组。你不需要复杂的php循环技巧来解释布局不好的表单标记,你只需要将表单标记更正为ne这是你想要的方式
这:
不要太聪明。保持简单。你能提供你的表单的样子吗?根据这一点,你甚至不需要做$.post(url,{users:postBody});
,而是使用$.post(url,postBody);
并更改PHP脚本accordingly@CodeGodie我可以动态添加更多的输入字段,因此我的表单可以长达50个输入字段(最多25个用户)。我将添加我的问题。如果这是供实际用户使用的生产代码,而不是某个学术项目,那么这里的明文密码是非常重要的。这不是生产代码,我所有的密码都是加密的。你能提供你的表单
的外观吗?根据这一点,你甚至可能不需要做$.post(url,{users:postBody});
,而您应该使用$.post(url,postBody);
并更改PHP脚本accordingly@CodeGodie我可以动态添加更多的输入字段,因此我的表单可以长达50个输入字段(最多25个用户)。我将添加我的问题。如果这是供实际用户使用的生产代码,而不是某个学术项目,那么这里的明文密码非常重要。这不是生产代码,我所有的密码都在加密。谢谢,但我仍然没有用此代码获得正确的数组。抱歉,你说得对。我搞错了。我更新了d答案。这是一个正在运行的演示。谢谢你,但我仍然没有用这段代码获得正确的数组。对不起,你是对的。我搞错了。我更新了答案。这是一个正在运行的演示,谢谢你,这给了我数组,因为我需要它。除了两个用户之后它停止工作,这很奇怪。这是因为计数错误。($i=0;$ifor($i=0;$i是它应该做的事情