Home > php教程 > php手册 > PHP实现数组递归转义的方法,php数组递归转义

PHP实现数组递归转义的方法,php数组递归转义

WBOY
Release: 2016-06-13 09:26:19
Original
802 people have browsed it

PHP实现数组递归转义的方法,php数组递归转义

本文以实例形式讲述了PHP实现数组递归转义的方法,分享给大家供大家参考之用。具体方法如下:

主要功能代码如下:

$arr = array('a"aa',array("c'd",array('e"f')));
function changes($arr){
 foreach($arr as $k=>$v){
 if (is_string($v)){
  $arr[$k] = addslashes($v);
 }else if (is_array($v)) { //若为数组,则再转义.
  $arr[$k] = changes($v);
 }
 }
 return $arr;
}
print_r(changes($arr));

Copy after login

希望本文所述对大家的PHP程序设计有所帮助。

PHP 递归改变二维数组的值的问题

//先来个check方法,得弄方法啊,不然不能递归 public function check($result, $arr, $r) { $re=$r; foreach ($result as $value) { if ($arr[$re]['name'] == $value['name'] || $arr[$re]['uid'] == $value['uid']) { $re = $re + 1; $this->check($result, $arr, $re); } } return $re; } //以下为主方法 public function xxxxxxxxxxxxxxxx() { $arr = array( array('id' => 1, 'name' => 'aaa', 'uid' => 1), array('id' => 2, 'name' => 'bbb', 'uid' => 2), array('id' => 3, 'name' => 'ccc', 'uid' => 3), array('id' => 4, 'name' => 'ddd', 'uid' => 4), array('id' => 5, 'name' => 'ccc', 'uid' => 3), array('id' => 6, 'name' => 'bbb', 'uid' => 2), array('id' => 7, 'name' => 'bbb', 'uid' => 2), array('id' => 8, 'name' => 'fff', 'uid' => 6), array('id' => 9, 'name' => 'ccc', 'uid' => 3), array('id' => 10, 'name' => 'bbb', 'uid......余下全文>>
 

php多维数组的键名递归

试试盲写能力
$a=//原数组;
$b=array();
foreach($a as $k=>$v)
$b[ intval($v['id']) ]=$v['unit_name'];
$a=$b;
print_r($a); //$a为修改后的关联数组
 

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template