Home >Backend Development >PHP Tutorial >动态生成变量并把生成的变量初步数组

动态生成变量并把生成的变量初步数组

WBOY
WBOYOriginal
2016-06-13 11:53:34919browse

动态生成变量并把生成的变量初始数组
如题

<?php <br />	//$b是变化的<br />	for($i=0;$i<$b;$i++){<br />		//生成$b个变量<br />	}<br />	//将变量放入数组<br />	$items_sum=array('变量1'=>'值1','变量2'=>'值2','变量3'=>'值3'...'变量$b'=>'值$b',);<br />	?>

求简单实例说明
------解决方案--------------------
$items_sum = array();<br />    //$b是变化的<br />    for($i=0;$i<$b;$i++){<br />        //生成$b个变量<br />       $items_sum[$i] = $i;//值 <br />    }

------解决方案--------------------
$b = 10;<br />$i = 0;<br />$old = array();<br />$old = get_defined_vars();<br /><br />for($i=0;$i<$b;$i++){<br />  ${"b_$i"} = $i;<br />}<br />$items_sum = array_diff_key(get_defined_vars(), $old);<br />print_r($items_sum);<br />var_dump($b_1, $b_2, $b_5);<br />

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