【问题标题】:PHP - How to return all the values of an array? [duplicate]PHP - 如何返回数组的所有值? [复制]
【发布时间】:2016-07-15 07:28:18
【问题描述】:

我是一个从谷歌获取相关关键字的跟随函数。

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

谢谢

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    使用implode()将数组转换成字符串为

    $arr=getKeywordSuggestionsFromGoogle('Faviana Sweetheart');
    
    echo $str = implode (", ", $arr);
    

    【讨论】:

      【解决方案2】:

      在php中我们可以使用implode()函数将array转换成string。你可以这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-07
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        • 2021-01-15
        相关资源
        最近更新 更多