本文以實例形式講述了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));
希望本文所述對大家的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為修改後的關聯數組