php:对象的数组(2)
作者:互联网
php:对象的数组(2)
一、php源码
<?php class NUM { public $number=""; public $number_class=""; } class obj { public $v1 = ""; public $v2 = ""; } for($i=0; $i<10; $i++) { $cat[$i] = new obj; $nm[$i] = new NUM; $nu[$i] = new NUM; $nm[$i] -> number=$i; $nm[$i] -> number_class = "red"; $nu[$i] -> number=$i*2; $nu[$i] -> number_class = "green"; // 将“对象”赋予“成员变量” $cat[$i]->v1 = $nm[$i]; $cat[$i]->v2 = $nu[$i]; } foreach($cat as $ct) { echo "v1-number=" . $ct->v1->number ." v1-class=". $ct->v1->number_class . "\t"; echo "v2-number=" . $ct->v2->number ." v2-class=". $ct->v2->number_class . PHP_EOL; } ?>
二、运行结果
标签:对象,number,v1,v2,数组,php,class,ct 来源: https://www.cnblogs.com/lnlidawei/p/16557449.html