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; } ?>
二、运行结果
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/php/279171.html