-
-
Save teresko/d7d8a3935c8e2c9d9c40 to your computer and use it in GitHub Desktop.
tree conversions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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