Php 带递归函数的嵌套循环?

Php 带递归函数的嵌套循环?,php,Php,我需要对google建议的每个结果进行递归循环,直到用户定义的深度,并将结果保存在多维数组中,稍后将对此进行探讨 我想得到这个结果 google google app google app store google app store games google app store games free google maps google maps directions google maps directions driving google maps directions driving c

我需要对google建议的每个结果进行递归循环,直到用户定义的深度,并将结果保存在多维数组中,稍后将对此进行探讨

我想得到这个结果

google
google app
google app store
google app store games
google app store games free
google maps
google maps directions
google maps directions driving
google maps directions driving canada
...
目前,我的递归函数返回第二次嵌套的复制结果

google
google app
google app
google app store
google app store
google app
google app store
google app store
google app store
...
我认为问题来自数组(父结果),我将其作为参数传递给我的函数recursive_function(),并传递给每个嵌套循环

$child = recursive_function($parent[0][1], $depth, $inc+1);
递归函数

// keywords at line or spaced
$keywords = explode("\n", trim("facebook"));

$result = recursive_function($keywords, 2);

function recursive_function($query, $depth, $inc = 1)
{
    $urls = preg_filter('/^/', 'http://suggestqueries.google.com/complete/search?client=firefox&q=', array_map('urlencode', $query));

    $parent = curl_multi_function($urls);

    array_multisort($parent[0][1]);

    if (count($parent[0][1]) === 0 || $inc >= $depth)
    {
        $out[] = $parent[0][1];
    }
    else
    {
        $child = recursive_function($parent[0][1], $depth, $inc+1);

        $out[] = $child;
    } 

    return $out;
}
函数旋度

function curl_multi_function($data, $options = array()) 
{
    // array of curl handles
    $curly = array();

    // data to be returned
    $result = array();

    // multi handle
    $mh = curl_multi_init();

    // loop through $data and create curl handles
    // then add them to the multi-handle
    foreach ($data as $id => $d) 
    {
        $curly[$id] = curl_init();

        $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
        curl_setopt($curly[$id], CURLOPT_URL,            $url);
        curl_setopt($curly[$id], CURLOPT_HEADER,         0);
        curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curly[$id], CURLOPT_SSL_VERIFYPEER, 0);

        // post?
        if (is_array($d)) 
        {
            if (!empty($d['post'])) 
            {
                curl_setopt($curly[$id], CURLOPT_POST,       1);
                curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
            }
        }

        // extra options?
        if (!empty($options)) {
          curl_setopt_array($curly[$id], $options);
        }

        curl_multi_add_handle($mh, $curly[$id]);
    }

    // execute the handles
    $running = null;
    do 
    {
        curl_multi_exec($mh, $running);
    } 
    while($running > 0);

    // get content and remove handles
    foreach($curly as $id => $c) 
    {
        $result[$id] = curl_multi_getcontent($c);

        // decode json result
        $result[$id] = json_decode(utf8_encode($result[$id]));

        curl_multi_remove_handle($mh, $c);
    }

    // all done
    curl_multi_close($mh);

    return $result;
}

谢谢

我稍微更改了您的递归函数:

function recursive_function($query, $depth, $inc = 1)
{
    $urls = preg_filter('/^/', 'http://suggestqueries.google.com/complete/search?client=firefox&q=', array_map('urlencode', $query));

    $parent = curl_multi_function($urls);

    foreach ($parent as $key => $value) {
      array_multisort($value[1]);

      $words = explode(' ', $value[0]);
      $lastWord = end($words);

      if (count($value[1]) === 0 || $inc >= $depth) {
          $out[$lastWord] = [];
      } else {
          unset($value[1][0]);
          $child = recursive_function($value[1], $depth, $inc+1);
          $out[$lastWord] = $child;
      } 
    }

    return $out;
}
它生成如下数组:

[
   google =>
     [
       app =>
         [
           store =>
             [
                games =>
                  [
                    free => []
                  ]
              ]
         ]
         ...
     ]
]

这就是你想要的吗?

所以。。。有什么问题吗?解释你有什么输入,每个深度元素应该有什么功能。@Justinas:我的问题更精确了。抱歉,我的英语不好。看起来
curl\u multi\u function()
返回了许多矩阵,但出于某种原因,您只关心第一个矩阵,这与您期望的输出不兼容,而且我也不明白为什么第一个建议只会将查询增加1个单词,我的测试表明,谷歌只需输入一个搜索词就可以建议整个句子。递归迭代器和getDepth()可以帮助我吗?你应该尝试将你的问题缩小到一个问题,并给我们一个答案。在我看来,您正在google上发起一系列搜索请求,因为您的
curl\u multi\u function()
将在每次调用
recursive\u function()
时被反复调用。这真的是你的意图吗?