Skip to content

Instantly share code, notes, and snippets.

@felipsmartins
Last active February 28, 2021 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save felipsmartins/b8a5a8f7a8547506c5a7 to your computer and use it in GitHub Desktop.
Save felipsmartins/b8a5a8f7a8547506c5a7 to your computer and use it in GitHub Desktop.
<?php
# StackOverflow related question: http://stackoverflow.com/questions/34886008/create-nested-array-by-array-of-key
# The @Gavriel's answer is pretty good!
# But it could be somewhat more "*easy to catch*" like this:
$new = $value;
foreach (array_reverse($keys) as $valueAsKey) $new = [$valueAsKey => $new];
# In fact, it's a version of @Gavbriel's answer using array_reverse() and some semantic variables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment