Home  >  Article  >  Backend Development  >  php里怎么把循环数组赋值给字符串变量

php里怎么把循环数组赋值给字符串变量

WBOY
WBOYOriginal
2016-06-23 13:57:311407browse

比如说

    $contents = curl_exec($ch);  	preg_match_all("/<td>(.*)<\/td>/",$contents,$arr);	for($i=0;$i<=100;$i++)		{			 			print_r($arr[1][$i]);			if (($i + 1) % 1 ==0)			{	echo '<br/>';	}		}


我现在不要打印$arr,而是把它放到字符串变量中


回复讨论(解决方案)

$txt = '';for($i=0;$i<=100;$i++) {  $txt .= $arr[1][$i]);  if (($i + 1) % 1 ==0) {    $txt .=  '<br/>';  }}

你附加  
 条件是 %1,也就是每项都加
所以不需要循环,直接 $txt = join('
', $arr[1]); 就可以了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn