2016-07-15 46 views
0

我是從Google獲取相關關鍵字的以下功能。PHP的 - 如何返回數組的所有值?

function getKeywordSuggestionsFromGoogle($keyword) { 
    $keywords = array(); 
    $data = file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.urlencode($keyword)); 
    if (($data = json_decode($data, true)) !== null) { 
     $keywords = $data[1]; 
    } 

    return $keywords; 
} 
function array_values_recursive($ary) { 
    $lst = array(); 
    foreach(array_keys($ary) as $k) { 
     $v = $ary[$k]; 
     if (is_scalar($v)) { 
      $lst[] = $v; 
     } elseif (is_array($v)) { 
      $lst = array_merge($lst,array_values_recursive($v)); 
     } 
    } 
    return $lst; 
} 
print_r(getKeywordSuggestionsFromGoogle('Faviana Sweetheart')); 

然而,從這個函數的輸出給數組作爲

Array ([0] => faviana sweetheart chiffon gown [1] => faviana sweetheart dress [2] => faviana sweetheart chiffon gown blue [3] => faviana sweetheart chiffon gown red [4] => faviana sweetheart chiffon gown black [5] => faviana sweetheart [6] => faviana sweetheart chiffon [7] => faviana sweetheart gown [8] => faviana strapless sweetheart dress [9] => faviana strapless sweetheart chiffon dress) 

我想知道如何能夠作爲字符串變量輸出

faviana sweetheart chiffon gown, faviana sweetheart dress, faviana sweetheart chiffon gown blue, ..., faviana strapless sweetheart chiffon dress 

謝謝

回答

2

使用implode()將數組轉換爲字符串爲

$arr=getKeywordSuggestionsFromGoogle('Faviana Sweetheart'); 

echo $str = implode (", ", $arr); 
1

在PHP中,我們可以使用implode()功能的array轉換成string。你可以這樣做:

$yourArray = getKeywordSuggestionsFromGoogle('Faviana Sweetheart'); 
$string = implode (", ", $yourArray); 
print_r ($string);