php 的fixedArray

标准库的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;
	} 
}

转载于:https://my.oschina.net/inuxor/blog/504510

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值