Home  >  Article  >  Backend Development  >  PHP数组的处理,高人帮忙,感谢

PHP数组的处理,高人帮忙,感谢

WBOY
WBOYOriginal
2016-06-13 12:19:26974browse

PHP数组的处理求助,高人帮忙,感谢。

本帖最后由 a858070363 于 2015-05-02 16:29:12 编辑 1、表单提交的数据为:值1,值2,值3,值4,值5,值6……
2、我用explode函数来处理表单提交内容,转为一个数组

我现在的问题是:

每3个去掉一个值,并把最后的数组转成最开始时的格式,如:值1,值2,值4,值5

用代码如何实现?

折腾两天了,实在搞不定,求帮忙。感谢。
------解决思路----------------------
<br /><?php<br /><br />$data = '值1,值2,值3,值4,值5,值6';<br />$dataArr = explode(',', $data);<br /><br />foreach ($dataArr as $key => $value) {<br />	<br />	if($key%3 == 2){<br />		unset($dataArr[$key]);<br />	}<br /><br />}<br /><br />$res = implode(',', $dataArr);<br />echo $res;<br />


拆开遍历再合并就好
------解决思路----------------------
本帖最后由 xuzuning 于 2015-05-02 17:46:32 编辑
$n = 5; //每 5 个<br />$s = '值1,值2,值3,值4,值5,值6,值7,值8,值9,值10,值11';<br /><br />$a = explode(',', $s);<br /> <br />foreach ($a as $k => $v) if($k % $n == $n - 1) unset($a[$k]);<br /> <br />$res = join(',', $a);<br />echo $res;
值1,值2,值3,值4,值6,值7,值8,值9,值11

不过隔几个和每几个的意思是不一样的。自己调整一下
------解决思路----------------------
$key%3 == 2 这个能看懂吗?

当将 3 写作 $n 时,2 就可写作 $n - 1 了吧?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn