I have two array values:
$values = ["Phillip", "Phil"];
In my set()
method, I want to return an array object that first has a defined name key and second nickname key, like:
array(2) {
"name" => "Phillip"
"nickname" => "Phil"
}
Is there a better way to do it? I am new at PHP, though.
function set(?array $values)
{
$items = [];
foreach ($values as &$item) {
$item['name'] = $items;
$item['nickname'] = $items;
}
}
CodePudding user response:
Like this
<?php
$values = ["Phillip", "Phil"];
function set(array $values): array
{
return [ "name" => $values[0], "nickname" => $values[1] ];
}
var_dump(set($values));
Result
array(2) {
["name"]=>
string(7) "Phillip"
["nickname"]=>
string(4) "Phil"
}