Skip to content

Instantly share code, notes, and snippets.

@veloxy
Last active August 29, 2015 13:56
Show Gist options
  • Save veloxy/9134387 to your computer and use it in GitHub Desktop.
Save veloxy/9134387 to your computer and use it in GitHub Desktop.
String to array
<?php
function stringToArray($key)
{
$result = array();
$parts = explode('.', $key);
$current = &$result;
for ($i = 0, $max = count($parts); $i < $max; $i++) {
if (!isset($current[$parts[$i]])) {
$current[$parts[$i]] = array();
}
if ($max == ($i+1)) {
$current[$parts[$i]] = 1;
}
$current = &$current[$parts[$i]];
}
return $result;
}
# input: This.Is.A.Test
# output: Array (
# [This] => Array (
# [Is] => Array (
# [A] => Array (
# [Test] => 1
# )
# )
# )
#)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment