将多个1维数组合并为一个多维数组

时间:2017-04-14 06:21:21

标签: php arrays multidimensional-array

使用以下数组作为输入:

  • 样式编号:Array ( [0] => 1001 [1] => 1002 [2] => 1003 )

  • 颜色:Array ( [0] => 'red' [1] => 'red' [2] => 'red' )

  • 小尺寸:Array ( [0] => 1 [1] => 1 [2] => 1 )

  • 大尺寸:Array ( [0] => 2 [1] => 2 [2] => 2 )

我需要创建一个这样的数组:

 array(
    '1001'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1002'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1003'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    )
)

5 个答案:

答案 0 :(得分:2)

如下所示(非常简单): -

$final_array = array();

foreach($style as $key=>$val){
  $final_array[$val]['color'] = $colors[$key];
  $final_array[$val]['small_size'] = $small_size[$key];
  $final_array[$val]['large_size'] = $large_size[$key];
}

echo "<pre/>";print_r($final_array);

输出: - https://eval.in/776784

更好的方法 : - https://eval.in/776787

答案 1 :(得分:1)

您可以使用for循环来实现此目的:

$arrayNumbers = array(1001, 1002, 1003);
$arrayColours = array('red', 'red', 'red');
$arraySmallSizes = array(1, 1, 1);
$arrayLargeSizes = array(2, 2, 2);

$output = array();

$count = count($arrayNumbers);
for($i = 0; $i < $count; $i++) {
    $output[$arrayNumbers[$i]] = array('color' => $arrayColours[$i], 'small_size' => $arraySmallSizes[$i],'large_size' => $arrayLargeSizes[$i]);
}

print_r($output);
// Array (
    [1001] => Array ( [color] => red [small_size] => 1 [large_size] => 2 )  
    [1002] => Array ( [color] => red [small_size] => 1 [large_size] => 2 ) 
    [1003] => Array ( [color] => red [small_size] => 1 [large_size] => 2 )
)

(见https://3v4l.org/9Wpj9表演)
或使用foreach循环:

$arrayNumbers = array(1001, 1002, 1003);
$arrayColours = array('red', 'red', 'red');
$arraySmallSizes = array(1, 1, 1);
$arrayLargeSizes = array(2, 2, 2);

$output = array();
foreach($arrayNumbers as $key => $value) {
    $output[$arrayNumbers[$key]] = array('color' => $arrayColours[$key], 'small_size' => $arraySmallSizes[$key],'large_size' => $arrayLargeSizes[$key]);
}

print_r($output);

(请参阅https://3v4l.org/ifv1o了解表现)

答案 2 :(得分:0)

你好你可以尝试这个我测试过的代码 -

$array1 = array( 0 => 1001  , 1 => 1002 ,2 => 1003 );
$array2 = array( 0 => 'red' ,1 => 'red' ,2 => 'red' );
$array3 = array( 0 => 1 ,1 => 1 ,2 => 1 );
$array4 = array( 0 => 2, 1 => 2 ,2 => 2 );
$final_array = [];
foreach($array1 as $key =>$value){
    $final_array[$value]['color'] =   $array2[$key];
    $final_array[$value]['small_size'] =   $array3[$key];
    $final_array[$value]['large_size'] =   $array4[$key];
}
print_r($final_array);

答案 3 :(得分:-1)

该代码应解决您的问题:

$input = array(
    '1001'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1002'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1003'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
);

$keys = array_keys($input);

$color = array_column($input, 'color');
$smallSize = array_column($input, 'small_size');
$largeSize = array_column($input, 'large_size');

答案 4 :(得分:-1)

您可以使用array_column

 public static MyInfo getInfo(MyInfo info)

<强>输出:

 $data = array(
    '1001'=>array(
        'color'=>'red',
        small_size=>1,
        large_size=>2,
    ),
    '1002'=>array(
        'color'=>'red',
        small_size=>1,
        large_size=>2,
    ),
    '1003'=>array(
        'color'=>'red',
        small_size=>1,
        large_size=>2,
    ),
);

var_dump(array_column($data, 'color'));

var_dump(array_column($data, 'small_size'));