vendredi 23 février 2018

Mapping value of an array as parameter to a function PHP

So I have a function:

$v->firstOrCreate([
    "test_page" => $vessel["test_page"],
    "deck" => $vessel->deck,
    "test_order" => $vessel->test_order,
    "component_name" => $vessel->component_name,
]);

And this code is repetitive but with different set of fields. So I try to reduce the code by a helper function: this will take an array of field and pass it as parameter to a function but also do some kind of mapping value inside that function. I know it is kinda confusing to describe and I am not sure if this is a thing. Is there any way in PHP I can do something like

$field = ["field1","field2","field3","field4","field5",]
$model->firstOrCreate(
    foreach ($field as $i) {
        $i => $data[$i];
    }
);

Thanks !



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire