使用PHP爆炸字符串

PHP Script:

$path = substr($_SERVER['REQUEST_URI'], 1);
$parts = explode('/', $path);
$count = count($parts);
echo "<script>console.log('URL Path: $path');</script>";
echo "<script>console.log('Parts Length: $count');</script>";
foreach($parts as $i =>$key) {
    $i >0;
    echo "<script>console.log('Part $i). $key');</script>";
}
echo "<script>console.log('Test: $key[3]');</script>";

Outcome:

Tested URL: domain.com/Projects/A/B/C

URL Path: Projects/A/B/C

Parts Length: 4

Part 0). Projects

Part 1). A

Part 2). B

Part 3). C

Test:

Question:

As you can see from my outcome, my echo "<script>console.log('Test: $key[3]');</script>"; attempt to log a specific part does not work. How can I get part[X] from my exploded string?

You have your value in parts (and not in $key outside the foreach)

    $path = substr($_SERVER['REQUEST_URI'], 1);
    $parts = explode('/', $path);
    $count = count($parts);
    echo "<script>console.log('URL Path: $path');</script>";
    echo "<script>console.log('Parts Length: $count');</script>";
    foreach($parts as $i =>$key) {
        $i >0;
        echo "<script>console.log('Part $i). $key');</script>";
    }

    echo "<script>console.log('Test: " . $parts[3] . " ');</script>";