How can the output of an external command be captured and stored in an array using exec in PHP?

To capture the output of an external command and store it in an array using exec in PHP, you can use the exec function to run the command and capture the output in a variable. Then, you can use the explode function to split the output into an array based on a delimiter, such as a newline character.

// Run the external command and capture the output
exec('your_external_command_here', $output);

// Initialize an empty array to store the output lines
$output_array = [];

// Split the output into an array based on a newline character
foreach ($output as $line) {
    $output_array[] = $line;
}

// Now $output_array contains the output of the external command as an array