Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将多个值分配给数组中的单个用户?_Php_Arrays - Fatal编程技术网

Php 如何将多个值分配给数组中的单个用户?

Php 如何将多个值分配给数组中的单个用户?,php,arrays,Php,Arrays,我有两组填充了值的数组。一个是另一个的两倍大。我需要使用PHP将多组值分配给其他数组中的单个用户 我将解释以下情况: $user=['Raj','Rahul','Scot','Virat']; $value=['1','2','3','4','5','6','7','8'] 这里,我需要为循环中的每个用户分配2个$value数组值。输出应如下所示: 'Raj'->1,2 'Rahul'->3,4 'Scot'->5,6 'Virat'->7,8 $user=['Ra

我有两组填充了值的数组。一个是另一个的两倍大。我需要使用PHP将多组值分配给其他数组中的单个用户

我将解释以下情况:

$user=['Raj','Rahul','Scot','Virat'];

$value=['1','2','3','4','5','6','7','8']
这里,我需要为循环中的每个用户分配2个
$value
数组值。输出应如下所示:

'Raj'->1,2
'Rahul'->3,4
'Scot'->5,6
'Virat'->7,8
$user=['Raj','Rahul','Scot','Virat'];
$value=['1','2','3','4','5','6','7','8'];

$output = [];
foreach($user as $key => $name){
   $output[$name] = [$value[$key*2], $value[$key*2 + 1]];
}
print_r($output); // <-- you will have here your array.
我该怎么做?

您可以这样做:

'Raj'->1,2
'Rahul'->3,4
'Scot'->5,6
'Virat'->7,8
$user=['Raj','Rahul','Scot','Virat'];
$value=['1','2','3','4','5','6','7','8'];

$output = [];
foreach($user as $key => $name){
   $output[$name] = [$value[$key*2], $value[$key*2 + 1]];
}
print_r($output); // <-- you will have here your array.
$user=['Raj','Rahul','Scot','Virat'];
$value=['1','2','3','4','5','6','7','8'];
$output=[];
foreach($user as$key=>$name){
$output[$name]=[$value[$key*2],$value[$key*2+1];
}
打印(输出);// 你可以试试这个-

$user=['Raj','Rahul','Scot','Virat'];

$value=['1','2','3','4','5','6','7','8'];

## generate , separated values from every 2 elements in $value array
$temp = array_chunk($value, 2); // Generate array chunks with 2 elements in the same order
$temp = array_map(function($v) {
    return implode(',', $v); // implode them with comma
}, $temp);

## Combine the user array (as key) && generated $temp array (as values)
$new = array_combine($user, $temp);

var_dump($new);
输出

array(4) {
  ["Raj"]=>
  string(3) "1,2"
  ["Rahul"]=>
  string(3) "3,4"
  ["Scot"]=>
  string(3) "5,6"
  ["Virat"]=>
  string(3) "7,8"
}
如果不需要
分隔值,则可以用
数组块($value,2)
替换
数组映射
函数

你可以试试这个

if ( count($value) !== (2*count($user) ) {
    print 'incorrect size';
}
else {
    $result = array(),
    for ($i=0, $j=0 ; $i<count($user) ; $i++, $j+=2) {
        $result[$user[$i]] = sprintf('%s,%s', $value[$j], $value[$j+1]);
    }
}
if(计数($value)!=(2*计数($user)){
打印“尺寸不正确”;
}
否则{
$result=array(),

对于($i=0,$j=0;$i您可以简单地使用
array\u chunk
array\u walk
,如下所示

$result = [];
$chunked_array = array_chunk($value, 2);
array_walk($chunked_array,function($v,$k) use (&$result,$user) {
    $result[$user[$k]] = implode(',',$v);
});
print_r($result);
或者您可以简单地使用
for
循环,就像

$result = [];
$count = count($value);
$j = 0;
for($i = 0; $i < $count; $i += 2){
    if(isset($user[$j]))
        $result[$user[$j++]] = $value[$i].",".$value[$i+1];
}
print_r($result);
$result=[];
$count=计数($value);
$j=0;
对于($i=0;$i<$count;$i+=2){
如果(isset($user[$j]))
$result[$user[$j++]=$value[$i]。”,“$value[$i+1];
}
打印(结果);
$user=['Raj','Rahul','Scot','Virat'];
$value=['1','2','3','4','5','6','7','8'];
$output=[];
foreach($user as$key=>$name){
$output[$name]=$value[$key*2]。“,”,“$value[$key*2+1];
}
回声“;
打印(输出);

如果结果需要多维数组,则只需将用户和分块值组合起来:

$result = array_combine($user,
                        array_map(function($v) { return implode(',', $v); },
                                  array_chunk($value, 2)));
如果需要逗号分隔的列表,则执行相同的操作,只需将
内爆()
映射到每个内部数组:


它在每一行中都会抛出许多语法错误。而且根据用户输入,值应该分配给用户。首先,它应该是
$user=array('Raj'、'Rahul'、'Scot'、'Virat');
这一行
$output[$name]=[$value[$key*2]、$value[$key*2+1]];
抛出错误。@subhra,您使用的是什么版本的php?对于现代版本,此代码是correct@subhra从PHP5.4开始,您还可以使用短数组语法,它将数组()替换为[]。再添加几行文字,介绍您的方法及其对OP的帮助。欢迎使用Stack Overflow!虽然此代码可能会回答此问题,但提供有关此代码为什么和/或如何回答此问题的其他上下文将提高其长期价值。
$result = array_combine($user,
                        array_map(function($v) { return implode(',', $v); },
                                  array_chunk($value, 2)));