$val) { if (!isset($val['prev'])) { $moji = $alphabet; } } $ans = $moji; while (true) { if (!isset($arr[$moji]['next'])) break; $moji = $arr[$moji]['next']; $ans .= $moji; } if ($cnt == 1) { echo $A . PHP_EOL; } elseif (strlen($ans) != $cnt) { // echo strlen($ans) . " != " . $cnt . PHP_EOL; echo "-1\n"; } else { echo $ans . PHP_EOL; }