类似['a','b','c','d','e','a','b','c','a','b','c','a','b','c']这样的有没有高级点的写法
2 回答
白衣染霜花
TA贡献1796条经验 获得超10个赞
functiongetArr($count){$str='abc';$arr=array();for($i=0;$i<$count;$i++){$rand=rand(0,2);$arr[]=$str[$rand];}}$count为你想要新建的数组的个数,主要利用能通过下标获取字符串的某个字符
慕容708150
TA贡献1831条经验 获得超4个赞
functionrepeatArr(arr,len,start){varret=[];for(vari=start||0;iret.push(arr[i%arr.length]); }returnret;}repeatArr('abcde'.split(''),10);//["a","b","c","d","e","a","b","c","d","e","a","b","c","d"]functionrepeatArr(arr,repeats){varret=[];for(vari=0;ifor(varj=repeats[i][0];j ret.push(arr[j%arr.length]); }}returnret;}repeatArr('abcde'.split(''),[[0,5],[0,3],[0,3]]);//["a","b","c","d","e","a","b","c","a","b","c"]
添加回答
举报
0/150
提交
取消