How to add an array inside an array array?

1
$regras = array(
    array(
        'field' => 'cpf',
        'label' => 'CPF',
        'rules' => 'required'
    ),
    array(
        'field' => 'senha',
        'label' => 'SENHA',
        'rules' => 'required|trim'
));
// o array $maisUmCriterio tem que ser inserido dentro de $regras
$maisUmCriterio =    
    array(
        'field' => 'nome',
        'label' => 'NOME',
        'rules' => 'required|trim'
);

// já tentei fazer

$regras = $regras + $maisUmCriterio;

// mas não funciona, preciso que o array $regras fique assim :

$regras = array(
    array(
        'field' => 'cpf',
        'label' => 'CPF',
        'rules' => 'required'
    ),
    array(
        'field' => 'senha',
        'label' => 'SENHA',
        'rules' => 'required|trim'
    ),
    array(
        'field' => 'nome',
        'label' => 'NOME',
        'rules' => 'required|trim'
    )
);
    
asked by anonymous 17.12.2016 / 13:56

1 answer

1

One thing missing, which is "[]" at the end of the variable name where you want to insert the new array:

We have:

$regras = array(
    array(
        'field' => 'cpf', 
        'label' => 'CPF',
        'rules' => 'required'
    ),
    array(
        'field' => 'senha',
        'label' => 'SENHA',
        'rules' => 'required|trim'
    ),
);

To add another array inside this array:

$regras[] = array(
    'field' => 'nome',
    'label' => 'NOME',
    'rules' => 'required|trim'
);

DEMONSTRATION

In fact you even have more methods:

array_unshift , add to the beginning of the array:

$add = array(
    'field' => 'nome',
    'label' => 'NOME',
    'rules' => 'required|trim'
);

array_unshift($regras, $add);

array_push , you add at the end (similar to the first example I gave):

$add = array(
    'field' => 'nome',
    'label' => 'NOME',
    'rules' => 'required|trim'
);

array_push($regras, $add);
    
17.12.2016 / 14:00