substr($str, 0, 1)
// or
$str{0}
will give you the first character of a string, or it would if your code worked. It won't because:
- You use "for" with integer indexes instead of "foreach", so that you try to access "$array[0]", "$array[1]", etc. when there are no such indexes in your array.
- "$array" in your function is not the same as your other "$array". Different scopes, different variables.
This will give you the first character of the array keys:
$array[";sc"] = "Site Content";
$array["rg"] = "Reg Link";
$array["/ts"] = "Not link";
drawLeftNavMenu($array);
function drawLeftNavMenu($array)
{
foreach ($array as $key => $value) {
echo substr($key, 0, 1) . '<br />';
}
}
although I can't say much for this way of structuring your data. It's probably going to cause you continual problems down the line.