Code:

$news = array(); foreach ($parse as $index => $value) { preg_match($value, $main_page, $matches); $news[$index] = $matches; } var_dump($news); 

I get this array:

 'name' => array (size=2) 0 => string '<h1 class="moviename-big" itemprop="name">Гладиатор</h1>' (length=65) 1 => string 'Гладиатор' (length=18) 'originalname' => array (size=2) 0 => string '<span itemprop="alternativeHeadline">Gladiator</span>' (length=53) 1 => string 'Gladiator' (length=9) 'year' => array (size=2) 0 => string 'год</td> <td><div style="position: relative"> <a href="/lists/m_act%5Byear%5D/2000/" title="">2000</a>' (length=141) 1 => string '2000' (length=4) 'country_title' => array (size=2) 0 => string 'страна</td> <td><div style="position: relative"> <a href="/lists/m_act%5Bcountry%5D/1/">США</a>, <a href="/lists/m_act%5Bcountry%5D/11/">Великобритания</a> </div></td>' (length=242) 1 => string '<div style="position: relative"> <a href="/lists/m_act%5Bcountry%5D/1/">США</a>, <a href="/lists/m_act%5Bcountry%5D/11/">Великобритания</a> </div>' (length=199) 'slogan' => array (size=2) 0 => string 'слоган</td><td style="color: #555">&laquo;Генерал, ставший рабом. Раб, ставший гладиатором. Гладиатор, бросивший вызов империи&raquo;</td></tr>' (length=219) 1 => string '&laquo;Генерал, ставший рабом. Раб, ставший гладиатором. Гладиатор, бросивший вызов империи&raquo;' (length=168) 

If I write this:

 $news = array(); foreach ($parse as $index => $value) { preg_match($value, $main_page, $matches); $news[$index] = $matches[1]; } var_dump($news); 

Then I get an Undefined offset: 1 . Why and how to fix?

  • one
    First check for an index: if (isset($matches[1])) { ... } - Gino Pane

2 answers 2

In one of the iterations, he does not find a group with index 1 from $ value in $ main_page. Respectively in matches [1] nothing will fall, but all expression will be only in $ matches [0], if there was a match. Above correctly written, check the existence of $ matches [1] before writing. Well, or from the category of bad advice - a crutch with @.

UPD for example

  $value = '/(\w+): (\d+)?/'; $main_page = 'News: 2016'; preg_match($value, $main_page, $matches); var_dump ($matches); 

Conclusion:

 array (size=3) 0 => string 'News: 2016' (length=10) 1 => string 'News' (length=4) 2 => string '2016' (length=4) $value = '/(\w+): (\d+)?/'; $main_page = 'News: zzz'; preg_match($value, $main_page, $matches); var_dump ($matches); 

Conclusion

 array (size=2) 0 => string 'News: ' (length=6) 1 => string 'News' (length=4) 

In the second variant there are no matches with the section (\ d +), respectively, matches [2] do not exist. Similarly, in some cases you have no coincidences between value and main_page. In the array with the results there is no element with index 1.

  • honestly did not understand what you can try to portray in the code. - Sergalas
  • @Sergalas and the var_dump array you var_dump is full, or is it part of it? Because it does not see a single sub-array of 1 element, which goes against the idea of ​​an answer. - Regent

You can do this by checking whether there were any coincidences at all:

 if (preg_match($value, $main_page, $matches)) { $news[$index] = $matches[1]; } 

http://php.net/manual/ru/function.preg-match.php

preg_match () returns 1 if the pattern parameter matches the passed subject parameter, 0 if not, or FALSE if an error occurs.