当结果为数组时从 php exec() 获取数组
Getting array from php exec() when result is array
我有以下代码:
$transcribe1 = exec('"C:\path\to\gcloud\gcloud.cmd" ml speech recognize "audio-file.flac" --language-code="en-GB"', $transcribe2);
echo '<pre>'; print_r($transcribe1); echo '</pre>';
echo '<pre>'; print_r($transcribe2); echo '</pre>';
$transcribeArray = json_decode($transcribe1, true);
echo '<pre>'; print_r($transcribeArray); echo '</pre>';
结果是:
Array
(
[0] => {
[1] => "results": [
[2] => {
[3] => "alternatives": [
[4] => {
[5] => "confidence": 0.880379,
[6] => "transcript": "the text returned from google cloud"
[7] => }
[8] => ]
[9] => }
[10] => ]
[11] => }
)
前两个print_r
return为空。
我想做的就是从 gCloud 获取 json returned 到一个数组中,这样我就可以在其余代码中正确引用它。换句话说,一个看起来像这样的数组:
[results] => Array(
[alternatives] => Array(
[confidence] => "0.880379",
[transcript] => "the text returned from google cloud"
);
);
我错过了什么?
通过更多的 Whosebug 搜索解决了这个问题。
exec
returns 它做的一整套东西
shell_exec
returns 只有执行的结果,以其原始格式。
我有以下代码:
$transcribe1 = exec('"C:\path\to\gcloud\gcloud.cmd" ml speech recognize "audio-file.flac" --language-code="en-GB"', $transcribe2);
echo '<pre>'; print_r($transcribe1); echo '</pre>';
echo '<pre>'; print_r($transcribe2); echo '</pre>';
$transcribeArray = json_decode($transcribe1, true);
echo '<pre>'; print_r($transcribeArray); echo '</pre>';
结果是:
Array
(
[0] => {
[1] => "results": [
[2] => {
[3] => "alternatives": [
[4] => {
[5] => "confidence": 0.880379,
[6] => "transcript": "the text returned from google cloud"
[7] => }
[8] => ]
[9] => }
[10] => ]
[11] => }
)
前两个print_r
return为空。
我想做的就是从 gCloud 获取 json returned 到一个数组中,这样我就可以在其余代码中正确引用它。换句话说,一个看起来像这样的数组:
[results] => Array(
[alternatives] => Array(
[confidence] => "0.880379",
[transcript] => "the text returned from google cloud"
);
);
我错过了什么?
通过更多的 Whosebug 搜索解决了这个问题。
exec
returns 它做的一整套东西
shell_exec
returns 只有执行的结果,以其原始格式。