2
During development, it is often the case that a request is made to the database, in which the returned data comes in array, sometimes it is necessary to perform a foreach
to adjust these data or even include more data according to a specific data.
Example :
$dadosUsuario = $this->UsuarioModel->getDados('all');
foreach($dadosUsuario as $k => $dadoUsuario){
$nome = $dadoUsuario['name'];
$lastName = $dadoUsuario['name'];
$fullName = sprintf('%s %s', $name, $lastName);
$dadoUsuario['fullName'] = $fullName;
$dadosUsuario[$k] = $dadoUsuario;
}
Note that it was necessary to reallocate the value of $dadosUsuario[$k]
I’d have some way of making it more dynamic?
You can use methods such as
array_walk()
: http://php.net/manual/en/function.array-walk.php orarray_walk_recursive()
: http://php.net/manual/en/function.array-walk-recursive.php– Ivan Ferrer