Preg_match_all returning multiple results but count only 1?

running preg_match_all yields:

Array ( [0] => Array ( [0] => 888446 [1] => 888447 [2] => 888448 [3] => 888449 [4] => 888450 [5] => 888451 [6] => 888453 [7] => 888454 [8] => 888455 [9] => 888472 [10] => 888473 [11] => 888456 [12] => 888457 [13] => 888474 ) )

however a foreach loop to read all elements only runs once and count() results in 1.

   $pos = preg_match_all($part_no1, $line, $matches, PREG_PATTERN_ORDER);

   if ($pos >= 1){   // if 01
     $k++;
     print_r($matches);
     foreach($matches as $match => $singleElement){
      echo $singleElement;
      $p++;
      echo "<br>";          
      echo $p;
     }

That’s because the $matches[0] element is an array of arrays. Use -

foreach($matches[0] as $value)
{
}

and -

echo count($matches[0]);

If you add echo '<pre>'; before the print_r($matches); statement, it will be easier to see what the data actually looks like.

Sponsor our Newsletter | Privacy Policy | Terms of Service