标准库的SplFixedArray结构
SPL,php标准库。好多拍黄片的完全不知道有这个玩意,也玩的挺好。php数组+foreach就包打一切了。 行是行,就是显得太Low,逼格不高一直是php为人诟病的所在。 Array,数组。原教旨主义者看到php的array会吓尿,这TM什么玩意,双链?hash?怎么看也不是数组啊。数组嘛看上去就应该是用整数的index访问的一组格式一致的结构,要先声明长度才行。 其实这种东西php标准库里就有。
SplFixedArray implements Iterator , ArrayAccess , Countable {
/* 方法 */
public __construct ([ int $size = 0 ] )
public int count ( void )
public mixed current ( void )
public static SplFixedArray fromArray ( array $array [, bool $save_indexes = true ] )
public int getSize ( void )
public int key ( void )
public void next ( void )
public bool offsetExists ( int $index )
public mixed offsetGet ( int $index )
public void offsetSet ( int $index , mixed $newval )
public void offsetUnset ( int $index )
public void rewind ( void )
public int setSize ( int $size )
public array toArray ( void )
public bool valid ( void )
public void __wakeup ( void )
}
:sweat_smile:一看这不就是一迭代器嘛。整那么高深。。。
玩法
写一个高逼格的99乘法表。。。。一层循环
<?php
$right = SplFixedArray :: fromArray(range(0, 9));
$left = SplFixedArray :: fromArray(range(1, 9));
for ($right->next(); ($right->valid() && $left->valid()) ; $right->next()) {
echo $left->current() . '*' . $right->current() . ' = ' . $left->current() * $right->current() . ' ';
if($right->current() == $left->current()){
$left->next();
$right->rewind();
echo PHP_EOL;
}
}