Home  >  Q&A  >  body text

The foreach loop of the two-dimensional array numeric string cannot be compared with a number. Try to convert it to an int type and the result will directly become 0.

Problem description: foreach loops the two-dimensional array to find content that meets the conditions and reorganizes it into a new two-dimensional array. The obtained digital string is compared with the number, and the comparison cannot be made. Try to convert the digital characters to int type, and all the results become into 0; (the following code can be run directly)

        //创建curl资源
        $ch=curl_init();
        //设置url和对应项
        curl_setopt($ch, CURLOPT_URL, "http://match.sports.sina.com.cn/football/csl/opta_rank.php?item=shoot&year=2014&lid=8&type=1&dpc=1");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //以文件形式返回而不是直接输出
        $data=curl_exec($ch);
        $my_file=fopen("shoot.html",'w') or die('no file');
        fwrite($my_file, $data);
        fclose($my_file);
        curl_close($ch);

        $my_file=fopen("shoot.html",'r');
        $res= fread($my_file,filesize("shoot.html"));
        fclose($my_file);
        
        //步骤2获取table内容转为数组
        $str1 = preg_replace("/<a[^>]*>/","", $res);
        $str2 = preg_replace("/<\/a>/","", $str1);
        preg_match('/<table[^>]*? class="tab01">(.*?)<\/table>/s',$str2,$match);
              
        preg_match_all('/<td.*?>.*?<\/td>/i',$match[0],$e1);
        
        foreach ($e1[0] as $k=>$v){
            $shoot_arr[ceil(($k+1)/8)-1][]=$v;
        }
        

        shuffle($shoot_arr);   //随机排序
         
        $left_10to50_arr=array();
        //var_dump($shoot_arr);
        foreach($shoot_arr as $keys=>$vals){  //
            $leftfoot=$shoot_arr[$keys][4];
            echo '<br/>';
             var_dump($leftfoot);
             echo '<br/>';
             var_dump((int)$leftfoot);   //强制转换为整型 数据变成0
             //获取满足条件的数据重组数组
            if($leftfoot >= 10 && $leftfoot<=50){  //不转换数据类型无法比较大小
                $left_10to50_arr[]=$vals;
            }
            if($keys>3)exit;
        }
        echo '<pre>';
        print_r($left_10to50_arr);
        echo '</pre>';
        exit;

The two-dimensional array format is as follows:

QQ图片20170929162436.png

After the numeric characters are converted, they become 0. The situation is as follows:

QQ截图20170929162647.jpg


焚天驻地焚天驻地2550 days ago1519

reply all(6)I'll reply

  • 朽木

    朽木2017-10-23 19:59:56

    What does it mean not enough beans? The dear friend above, p bean?

    reply
    0
  • phpcn_u38476

    phpcn_u384762017-10-02 23:35:02

    333

    reply
    0
  • phpcn_u38476

    phpcn_u384762017-10-02 23:34:54

    44444444444444433333333333

    reply
    0
  • phpcn_u38476

    phpcn_u384762017-10-02 23:22:52

    Want 1 bean 2 bean 3 bean 0 bean 0 bean - want = bean 43 bean 2 bean 2 bean 4 bean 4 bean

    reply
    0
  • phpcn_u38476

    phpcn_u384762017-10-02 23:14:58

    Not enough beans, not enough beans, not enough beans, not enough beans, not enough beans, not enough beans, not enough beans

    reply
    0
  • phpcn_u38476

    phpcn_u384762017-10-02 23:14:40

    Not enough beans, not enough beans, not enough beans, not enough beans, not enough beans, not enough beans

    reply
    0
  • Cancelreply