mercredi 2 décembre 2015

Laravel 5.1: Flatten Multidimensional array without removing keys

I'm trying to find a way to flatten a multidimensional array that doesn't remove the ID that is saved as a key.

I used this in my controller:

$dataslots = Dataslot::all()->map(function($dataslot) { return [$dataslot->id => $dataslot->maand->format('d-m-Y')]; });

The output of this is a multidimensional array that looks like:

Collection {#242 ▼
#items: array:4 [▼
0 => array:1 [▼
  1 => "01-12-2015"
 ]
1 => array:1 [▼
  2 => "01-01-2016"
 ]
2 => array:1 [▼
  3 => "01-02-2016"
 ]
3 => array:1 [▼
  4 => "01-03-2016"
 ]
]
}

I tried to use the Laravel Helper Function 'Array Flatten'. This method gives me the following result:

array:4 [▼
0 => "01-12-2015"
1 => "01-01-2016"
2 => "01-02-2016"
3 => "01-03-2016"
]

As you can see, the ID's which served as keys for my dates have been removed.

The result that I am looking for:

Collection {#243 ▼
#items: array:4 [▼
1 => "01-12-2015"
2 => "01-01-2016"
3 => "01-02-2016"
4 => "01-03-2016"
]
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire