具有递归功能的嵌套循环?

时间:2018-07-19 06:53:25

标签: 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 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;
}

谢谢

1 个答案:

答案 0 :(得分:1)

我对您的recursive_function做了一些更改:

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 => []
                  ]
              ]
         ]
         ...
     ]
]

这就是你想要的吗?