Skip to content

Instantly share code, notes, and snippets.

@lucianovalenca
Forked from felipsmartins/reverse20160119.php
Created February 28, 2021 16:57
Show Gist options
  • Save lucianovalenca/dacc1beeb82dd35ccc2e1cb9479771ec to your computer and use it in GitHub Desktop.
Save lucianovalenca/dacc1beeb82dd35ccc2e1cb9479771ec 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