Home  >  Article  >  Backend Development  >  求段代码:判断数组里是不是有重复内容,如果有就只更新指定数量,没有就添加

求段代码:判断数组里是不是有重复内容,如果有就只更新指定数量,没有就添加

WBOY
WBOYOriginal
2016-06-13 12:01:40872browse

求段代码:判断数组里是否有重复内容,如果有就只更新指定数量,没有就添加
内容:

一年级 X 2, 二年级 X 10 ,三年级 X 5 ……

判断里面是否有二年级,如果有就加5 结果:一年级 X 2, 二年级 X 15 ,三年级 X 5 ……
------解决方案--------------------

$qq = "一年级 X 2, 二年级 X 10 ,三年级 X 5 ";<br />$k = '二年级';<br />echo preg_replace("/(?<=$k X )\d+/e", '$0 + 5', $qq);<br />
一年级 X 2, 二年级 X 15 ,三年级 X 5 

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