在php中打印匹配的数组字符串值

时间:2016-06-03 18:11:31

标签: php arrays simple-html-dom

我正在尝试打印coupon_name的数组值。当我尝试这段代码时,

<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.couponrani.com/');

$coupon_name = array();

foreach($html->find('img') as $element) {
        $coupon_name[] = $element->alt . '<br>';
$find = in_array('coupon',$coupon_name);
  if ($find == false ) { 
        print_r($coupon_name);
  }

 }
    ?>

但是,我得到的结果是:例如我的数组有(1,2,3,4,5,6,7,8,9)这个值,

Array ( [0] => 1
) Array ( [0] => 1
[1] => 2
) Array ( [0] => 1
[1] => 2
[2] => 3
) Array ( [0] => 1
[1] => 2
[2] => 3
[3] => 4
) Array ( [0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
) Array ( [0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
像这样,例如最多9个。

但是,它需要的是什么

1
2
3
4
5
6
7
8
9

指导我,我的错误是什么。因为,我对php很新鲜。

1 个答案:

答案 0 :(得分:1)

更改

foreach($html->find('img') as $element) {
        $coupon_name[] = $element->alt . '<br>';
$find = in_array('coupon',$coupon_name);
  if ($find == false ) { 
        print_r($coupon_name);
  }

 }

为:

   foreach($html->find('img') as $element) {
                $coupon_name[] = $element->alt . '<br>';
                $result = array();      
    $find = in_array('coupon',$coupon_name);
      if ($find == false ) { 
            array_push($result,$coupon_name);
      }
     }
print_r($result);