vendredi 5 octobre 2018

PHP/Laravel - Create an array of objects from two simple arrays or an associative array that combines that two

I'm looking for a way transform a php associative array into an array of object and keying each association. I could also treat this as two separate simple arrays, one with the names and one with the classes. Here's an associative example...

array:2 [
  "someName" => "someClass"
  "someOtherName" => "someOtherClass"
]

Or

names => [0 => 'name1', 1 => 'name2']
classes => [0 => 'class1', 1 => 'class2']

...either way, I'm looking for an end result like this:

[
    { 'name': 'someName', 'class': 'someClass' },
    { 'someOtherName': 'someOtherClass' }
]

What's the smartest way to do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire