Function for adding multidimensional array elements

0

I'm new to PHP. I would like to create a function in on being called increment a line in the multidimensional array in PHP. Example:

//Array
$data = array(
    array('name' => 'Jack', 'ano' => 2002, 'cidade' => 'SÃO PAULO')
);
//Função
function adicionar($nome, $anoNascimento, $cidade){
...
}
//chama função
adicionar("PETER", "2000", "RIBEIRÃO PRETO");

//novo resultado do array
$data = array(
    array('name' => 'Jack', 'ano' => 2002, 'cidade' => 'SÃO PAULO'),
    array('name' => 'PETER', 'ano' => 2000, 'cidade' => 'RIBEIRÃO PRETO')
);

What is the best way to do this? Thanks in advance

    
asked by anonymous 03.07.2018 / 18:00

3 answers

2

There is already a PHP function that performs part of this task, array_push() . Read the documentation .

//Função

function adiciona($array,$nome,$ano,$cidade){
    $pessoa = array('name' => $nome, 'ano' => $ano, 'cidade' => $cidade);

    array_push($array,$pessoa);

    return $array;
}

//Utilização

$data = array(
    array('name' => 'Jack', 'ano' => 2002, 'cidade' => 'SÃO PAULO')
);

$data = adiciona($data,"PETER", "2000", "RIBEIRÃO PRETO");
    
03.07.2018 / 18:20
1

Do you want to create an array that serves as a "database"?

But, taking that question, you, with a function, which is what you want, can do exactly like this:

<?php

//Array
$data = array(
    array('name' => 'Jack', 'ano' => 2002, 'cidade' => 'SÃO PAULO')
);

function addNewElement ($nome, $anoNasc, $cidade){
    $GLOBALS['data'][] = ['name' => $nome, 'ano' => $anoNasc, 'cidade' => $cidade]; 

}

addNewElement('Lucas de Carvalho', 1998, 'Rio de Janeiro');

print_r($data);

It will be printed:

Array ( [0] => Array ( [name] => Jack [ano] => 2002 [cidade] => SÃO PAULO ) [1] => Array ( [name] => Lucas de Carvalho [ano] => 1998 [cidade] => Rio de Janeiro ) )
    
03.07.2018 / 18:32
1

The other answer indicated array_push , but I would say that it would be better (for being a language constructor, not a function) and more readable to use empty brackets to add values.

Example:

$data = array(
    array('name' => 'Jack', 'ano' => 2002, 'cidade' => 'SÃO PAULO')
);

$data[] = array('name' => 'Wallace', 'ano' => 1990', 'cidade' => 'Belo Horizonte');

The question I would ask is: Do you really need a function to do this? Would not it be just the case of using an operator to do such a thing?

Well, if you really need it, I would use a function, but instead of using the return, I would pass array as a reference.

$array = [];
$array[] = ['nome' => 'Bacco', 'ano' => 1900, 'cidade' => 'Interior de SP'];

function adicionar(array &$array, $nome, $ano, $cidade) {
     $array[] = compact('nome', 'ano', 'cidade');
}

adicionar($array, 'Wallace', 1990, 'BH');

Explanation:

  • When using the & operator, you will not be held hostage by the function return, so you can change the value of the array source directly.

  • The function compact sends the variables of the current scope with the names passed by parameter to a array .

  • array &$array indicates that the variable must be of type array and must be an already existing variable, which will be affected as a reference within the function. To understand a little more, read about passing by reference

Another curiosity: In PHP, after version 5.4, you do not need to use the keyword array() to declare them, you can use brackets ...

Example:

$data = [
     ['nome' => 'nome', 'valor' => 'valor']
];
    
03.07.2018 / 18:30