Function to add Multidimensional array elements

Asked

Viewed 946 times

0

I’m new to PHP. I would like to create a function in when 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’s the best way to do this? Thanks in advance

  • You want to create an array that serves as "database"?

3 answers

2


There is already a PHP function that performs part of this task, the array_push(). Give a read on 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");
  • Thank you very much Matheus

2

The other reply indicated the array_push, but I would say it would be better (by being a language builder, 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? Wouldn’t it just be the case to use an operator to do such a thing?

Well, if you really need it, I would use a function, but instead of using the feedback, I would pass the 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 to the return of the function and thus can change the value of the array of direct origin.

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

  • array &$array indicates that the variable must be of the type array and has to be an existing variable, which will be affected as reference within the function. To understand a little more, read on reference passage

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

Example:

$data = [
     ['nome' => 'nome', 'valor' => 'valor']
];

1

You want to create an array that serves as "database"?

But other than 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);

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 ) )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.