php - Result saves in two arrays -


i'm playing around foreach , simple html dom there i'm trying save down links array. problem result saves in 2 arrays instead of 1 array.

foreach($html->find('div[class^=voucher success]') $q)     {         @$var = $q->find('a', 0)->href;          $pos = strpos($var, "/ut/");          if($pos === false)         {             $item[] = $var;         }          var_dump($item);     } 

dump:

array(1) {  [0]=> string(10) "/hm?v=2726" }  array(2) {  [0]=> string(10) "/hm?v=2726" [1]=> string(10) "/hm?v=2732"  } 

why that? have done wrong?

it's not saving in 2 arrays. you're dumping data @ end of every foreach loop. therefore dumps twice, because there 2 loops in foreach.

to see final result of $item need dump after foreach.

foreach($html->find('div[class^=voucher success]') $q)     {         @$var = $q->find('a', 0)->href;          $pos = strpos($var, "/ut/");          if($pos === false)         {             $item[] = $var;         }     }  var_dump($item); 

the output be:

array(2) {  [0]=> string(10) "/hm?v=2726" [1]=> string(10) "/hm?v=2732"  } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -