2017-07-31 54 views
-1

我有这样的功能:Laravel,数组未定义?

public function handle() 
    { 
     News::truncate(); 
     $client = new Client(); 
     $crawler = $client->request('GET', 'features'); 
     $crawler->filter('div[id=content]>.homefeature')->each(function ($node, $key) { 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     if ($image = $node->filter('img')->count() > 0) { 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     $image = $node->filter('img')->image(); 
     $image_s = $image->getUri(); 
     $image_s = preg_replace("/thumb_/", "", $image_s); 
     $filename = basename($image_s); 
     $image_path = ('news-gallery/' . $filename); 
     Image::make($image_s)->save(public_path('news-gallery/' . $filename)); 
     $id = 1+ $key + 1; 
     $news = News::where('id', $id)->first(); 
     // if news is null 
     if (!$news) { 
      $news = new News(); 
     } 
     $news->title = $title; 
     $news->datepublished = $datepublished; 
     $news->description = $description; 
     $news->link = $link_r; 
     $news->image = $image_path; 
     $news->save(); 
     $this->info('Main article ' .$title. ' done succesfully'); 
    }); 
    $crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) { 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     $image = $node->filter('img')->image(); 
     $image_s = $image->getUri(); 
     $image_s = preg_replace("/thumb_/", "", $image_s); 
     $filename = basename($image_s); 
     $image_path = ('news-gallery/' . $filename); 
     Image::make($image_s)->save(public_path('news-gallery/' . $filename)); 
     //$id = 1+ $key + 1; 
     $newsArray[] = [ 
      'title' => $title, 
      'datepublished' => $datepublished, 
      'description' => $description, 
      'link' => $link_r, 
      'image' => $image_path, 
      ];  
     $this->info('Scraping done for ' .$title. ' succesfully'); 
    }); 
    print_r($newsArray); 
    //$news = News::insert($newsArray); 
    } 
} 

所以我试图把一切都放到一个数组保存前,但我得到一个错误说$ newsArray是不确定的?如果我把print_r放到函数中,就在它下面,我会得到一个输出。这怎么解决?我想要做的是循环throught每个结果象现在这样,将其插入到一个数组,后来,批量插入它DB

//例:

public function handle() 
    { 
    /* Clear table, set array of id's, and get today's date */ 
    Film::truncate(); 
    $arrayOfCinemaIds = [10565,7311,9434]; 
    $startdate = strtotime("today"); 
    $date = date("Y-m-d", $startdate); 
    /* Set date to today + + days */ 
    $endate = strtotime("+6 Days"); 
    /* Check if current date is less than $endate */ 
     while ($startdate < $endate) { 
     /* If it is less, do a foreach loop for $arrayOfCinemaIds */ 
      foreach($arrayOfCinemaIds as $id){ 
      /* Get data from cinelist api */ 
       $data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/'.$id.'/?day='.$date); 
       $data = json_decode($data); 
       /* Find entities that match api_key */ 
       $find_id = Entity::where('api_key', $id)->get(); 
        foreach($find_id as $id_found) { 
         $entity_id = $id_found->id; 
          foreach($data->listings as $listing) { 
           /* Put each record into an array */ 
           $title = $listing->title; 
           $time = implode(', ', $listing->times); 
          $films[] = [ 
           'title' => $title, 
           'times' => $time, 
           'entity_id' => $entity_id, 
           'date' => $date, 
           ]; 
          } 
         } 
        } 
      /* Increase $startdate by 1 day, display message, and change $date to new date */ 
      $startdate = strtotime("+1 Days", $startdate); 
      $this->info('Scraping for ' .$date. ' done, moving to next one'); 
      $date = date("Y-m-d", $startdate); 
     } 
     /* Insert array of films to DB, display message */ 
     $film = Film::insert($films); 
     $this->info('Succesfully Added Films to DB!'); 
    } 

我有这等工匠命令,而且这个工作非常好,那么为什么不是上面那个?

+0

声明数组$ newsArray = array(); – JYoThI

+0

声明你的'$ newsArray = array();'函数外部,并在函数 –

回答

0

您已经在每个函数中定义了$newsArray数组,并试图访问该函数的外部。所以,遇到了这个错误。

所以,在each()功能

$newsArray = array(); 

声明数组,那么你可以使用它。

UPDATE

您可以定义阵列等之后使用array_push()each()功能,

$element = [ 
     'title' => $title, 
     'datepublished' => $datepublished, 
     'description' => $description, 
     'link' => $link_r, 
     'image' => $image_path, 
     ]; 

array_push($newsArray,$element); 

然后你可以使用$newsArray插入数据。

希望你能理解。

+0

内使用它,它现在执行print_r,但看起来数组是空的 –

+0

@PrzemekWojtas使用该函数的引用,如函数($ node,$ key)use(&newsArray) – vijaykumar

+0

@PrzemekWojtas您需要在'each()'函数之前添加上面的行,并且不需要更改其他代码。 –

0

申报数组,并使用refrence

$newsArray = [] 
$crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) use(&newsArray) { 
    //Your loop code. 
}); 
+0

还是空数组..看到我的编辑,请 –

0

我想你可以试试这个:

$newsArray = array(); 
    $crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) { 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     $title = $node->filter('.plain')->text(); 
     $datepublished = $node->filter('.dateonline')->text(); 
     $description = $node->filter('.teaser-link')->text(); 
     $link = $node->filter('a')->link(); 
     $link_r = $link->getUri(); 
     $image = $node->filter('img')->image(); 
     $image_s = $image->getUri(); 
     $image_s = preg_replace("/thumb_/", "", $image_s); 
     $filename = basename($image_s); 
     $image_path = ('news-gallery/' . $filename); 
     Image::make($image_s)->save(public_path('news-gallery/' . $filename)); 
     //$id = 1+ $key + 1; 
     $newsArray = [ 
      'title' => $title, 
      'datepublished' => $datepublished, 
      'description' => $description, 
      'link' => $link_r, 
      'image' => $image_path, 
      ];  
     $this->info('Scraping done for ' .$title. ' succesfully'); 
    }); 
    //print_r($newsArray); 
    $news = News::insert($newsArray); 

希望这对你的工作!