Skip to content

Instantly share code, notes, and snippets.

@teresko

teresko/test.php Secret

Created September 24, 2014 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teresko/d7d8a3935c8e2c9d9c40 to your computer and use it in GitHub Desktop.
Save teresko/d7d8a3935c8e2c9d9c40 to your computer and use it in GitHub Desktop.
tree conversions
<?php
function makeLeafs($list)
{
$result = [];
if (is_string($list)) {
return [$list => null];
}
foreach ($list as $key => $value) {
if (is_int($key)) {
$result[$value] = null;
continue;
}
$result[$key] = makeLeafs($value);
}
return $result;
}
$input = ['foo'];
var_dump(makeLeafs($input));
$input = ['foo', 'bar'];
var_dump(makeLeafs($input));
$input = ['foo' => 'bar'];
var_dump(makeLeafs($input));
$input = ['foo' => ['bar', 'buz']];
var_dump(makeLeafs($input));
$input = ['foo' => ['bar' => 'buz']];
var_dump(makeLeafs($input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment