samedi 23 janvier 2016

Loop over multidimensional array and use parent key as prefix

When using Eloquent in Laravel 4 or 5, the model->with() methods uses the method as relation name. So if we get a user with his profile, that will return

$user = [
  'id' => 1,
  'name' => 'Example',
  'profile' => [
    'last_login' => '10-10-1900',
    'another_field' => 'with some data',
    'actions' => [
      0 => 'Logged in',
      1 => 'Logged out',
      2 => 'Signed up for news letter'
    ]
  ]
];

I want to flatten the full array and use the key of the relation as prefix, so i.e.:

$user = [
  'id' => 1,
  'name' => 'Example',
  'profile' => [
  'profile_last_login' => '10-10-1900',
  'profile_another_field' => 'with some data',
  'profile_actions' => [
    0 => 'Logged in',
    1 => 'Logged out',
    2 => 'Signed up for news letter'
    ]
  ]
];

How can i achieve this?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Qlp91K
via IFTTT

Aucun commentaire:

Enregistrer un commentaire