如何在php中将多个数组组合成一个数组?

时间:2016-11-26 19:13:20

标签: php arrays

早上好,以下数组想知道如何将它们组合成一个数组吗?

Array(
[name_option] => Tamanho
[name_option_value] => Grande) Array(
[name_option] => Cor
[name_option_value] => Amarelo) Array(
[name_option] => Tamanho
[name_option_value] => Medio) Array (
[name_option] => Cor
[name_option_value] => Azul) Array (
[name_option] => Tamanho
[name_option_value] => Pequeno ) Array(
[name_option] => Cor
[name_option_value] => Verde )

2 个答案:

答案 0 :(得分:0)

$a1=array("red","green");
$a2=array("blue","yellow");
$new_array = array_merge($a1,$a2);
print_r($new_array); 

http://www.w3schools.com/php/func_array_merge.asp

的更多详情

答案 1 :(得分:0)

你可以将它们组合成一个多维数组:

<?php
$a = [];
$a[] = [
    'name_option' => 'Tamanho',
    'name_option_value' => 'Grande'
];
$a[] = [
    'name_option' => 'Cor',
    'name_option_value' => 'Amarelo'
];
$a[] = [
    'name_option' => 'Tamanho',
    'name_option_value' => 'Medio'
];
$a[] = [
    'name_option' => 'Cor',
    'name_option_value' => 'Azul'
];
$a[] = [
    'name_option' => 'Tamanho',
    'name_option_value' => 'Pequeno'
];
$a[] = [
    'name_option' => 'Cor',
    'name_option_value' => 'Verde'
];

print_r($a);

结果:

Array
(
    [0] => Array
        (
            [name_option] => Tamanho
            [name_option_value] => Grande
        )

    [1] => Array
        (
            [name_option] => Cor
            [name_option_value] => Amarelo
        )

    [2] => Array
        (
            [name_option] => Tamanho
            [name_option_value] => Medio
        )

    [3] => Array
        (
            [name_option] => Cor
            [name_option_value] => Azul
        )

    [4] => Array
        (
            [name_option] => Tamanho
            [name_option_value] => Pequeno
        )

    [5] => Array
        (
            [name_option] => Cor
            [name_option_value] => Verde
        )

)

或重命名密钥(例如添加索引)以防止覆盖