[英]How to store all data with same key together
I have an array which looks like this: 我有一个看起来像这样的数组:
Array
(
[companyid] => 22
[userid] => Array
(
[0] => 60
[1] => 61
[2] => 65
[3] => 63
[4] => 64
[5] => 66
[6] => 68
[7] => 69
[8] => 70
[9] => 71
)
[username] => Array
(
[0] => nu1234
[1] => nu12345
[2] => username1235
[3] => testtttttt
[4] => username123
[5] => nu123
[6] => nu
[7] => sdgsdgsdg
[8] => testtttttt1234
[9] => nu1235678910
[10] =>
)
[password] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
)
)
How can I merge the same keys together, and for all of them add the first value which in this case is 22? 如何将相同的键合并在一起,并为所有键添加第一个值(在这种情况下为22)?
Needed end result: 所需的最终结果:
Array
(
[0] => Array
(
[companyid] => 22
[userid] => 35
[username] => dfhdfhdf
[password] => dfhdfhdfhdf
)
[1] => Array
(
[companyid] => 22
[userid] => 35
[username] => dfhdfhdf
[password] => dfhdfhdfhdf
)
[2] => Array
(
[companyid] => 22
[userid] => 35
[username] => dfhdfhdf
[password] => dfhdfhdfhdf
)
)
I've tried multiple things but nothing gives me the desired result. 我已经尝试了多种方法,但是没有任何结果能达到我想要的结果。
Before when I had only two values I did this: 在只有两个值之前,我这样做:
foreach($useroutput['username'] as $key => $val){
if(!empty($val)){
$users[] = array_combine(['username','password'],array_column($useroutput, $key));
}
}
But I cannot get it to work for more than 2 values. 但是我不能让它用于两个以上的值。 The array just shows empty when I print it.
当我打印该数组时,它显示为空。
I've also tried multiple combinations of the code below: 我还尝试了以下代码的多种组合:
foreach($useroutput as $key => $val){
$newarray[$key][]['companyid'] = $useroutput['companyid'];
$newarray[$key][]['userid'] = $useroutput['userid'];
$newarray[$key][]['username'] = $useroutput['username'];
$newarray[$key][]['password'] = $useroutput['password'];
}
array_merge
also didn't do what I need. array_merge
也没有做我所需要的。
$new_array = [];
foreach ($useroutput['userid'] as $key => $val) {
$new_array[] = [
'companyid' => $useroutput['companyid'],
'userid' => $val,
'username' => $useroutput['username'][$key],
'password' => $useroutput['password'][$key],
];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.