Home  >  Article  >  php教程  >  JSON取出及修改

JSON取出及修改

PHP中文网
PHP中文网Original
2016-05-22 18:26:59923browse

很多地方存放内容不再是单一值,而是很多数据合并后存放在一个字段里,为了更方便取出来更新或者再增加,于是写了一个结构函数,程序会持续优化,记录下来备用:
大家指点下,代码写的不好请各位指教;

	function editor_json_code($array, $code, $aid) {
		if(empty($code) OR empty($aid)) {
			return $array;
		}
		$json = json_decode($array, true);
		foreach($json as $key => $value) {
			if($key == $code) {
				$var[$key] = $aid;
			} else {
				$var[$key] = $value;
			}
		}
		if(!preg_match("/$code/", $array)) {
			$var[$code] = $aid;
		}
		return json_encode($var);
	}

                   

 以上就是JSON取出及修改的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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