替换中用到的最核心的一个方法
/**
* 把$base的某些元素替换成$replacements* 本来php库中是有这个函数的,不知道为什么就是调用不起,所以就copy过来啦,希望不要出错了。
*
* @param $base 需要被替换的数组
* @param $replacements 替换内容
* @return array 替换之后的数组
* @example $base = array('citrus' => array( "orange") , 'berries' =>
* array("blackberry", "raspberry"), );
* $replacements = array('citrus' => array('pineapple'), 'berries'
* => array('blueberry'));
* $basket = array_replace_recursive($base, $replacements);
* print_r($basket);
*
*/
function array_replace_recursive($base, $replacements) {
foreach ( array_slice ( func_get_args (), 1 ) as $replacements ) {
$bref_stack = array (
&$base
);
$head_stack = array (
$replacements
);
do {
end ( $bref_stack );
$bref = &$bref_stack [key ( $bref_stack )];
$head = array_pop ( $head_stack );
unset ( $bref_stack [key ( $bref_stack )] );
foreach ( array_keys ( $head ) as $key ) {
if (isset ( $key, $bref ) && is_array ( $bref [$key] ) && is_array ( $head [$key] )) {
$bref_stack [] = &$bref [$key];
$head_stack [] = $head [$key];
} else {
$bref [$key] = $head [$key];
}
}
} while ( count ( $head_stack ) );
}
return $base;
}
替换方法开始
/**
* 相对地址改为绝对地址
*
*
*/
public function replaceImg($data, $keys = array()) {
if (! empty ( $data ) && is_array ( $data [0] )) {
foreach ( $keys ["param"] as $k => $v ) {
if (! is_array ( $v )) {
if (! empty ( $data [0] [$v] )) {
$url = $data [0] [$v];
if (false === strpos ( strtolower ( $url ), 'http://' ) && false === strpos ( strtolower ( $url ), 'https://' )) {
$data [0] [$v] = $this->SOURCE_PATH . $data [0] [$v];
}
}
} else {
// 前缀地址,
if (empty ( $v ["preUrl"] )) {
$preUrl = $this->SOURCE_PATH;
} else {
$preUrl = $v ["preUrl"];
}
// 获取元素key,如果是多级的,则用点隔开
$fild = $v ["fild"];
$filds = array (
$fild
);
$f_data = $data [0];
if (strpos ( $fild, "." )) {
$filds = explode ( ".", $fild );
}
try {
// 取值
foreach ( $filds as $f ) {
if (! empty ( $f_data [$f] )) {
$f_data = $f_data [$f];
} else {
continue 2;
}
}
// 处理数据
if (! empty ( $f_data ) && false === strpos ( strtolower ( $f_data ), 'http://' ) && false === strpos ( strtolower ( $f_data ), 'https://' )) {
$f_data = $preUrl . $f_data;
} else {
continue;
}
} catch ( Exception $e ) {
continue;
}
// 赋值
$d_out = $f_data;
// 反转数组
$filds = array_reverse ( $filds );
foreach ( $filds as $f ) {
$d_out = array (
$f => $d_out
);
}
// 合并数组
// $data [0] = array_merge_recursive ( $data [0], $d_out );//合并之后,会多一级数组
$data [0] = $this->array_replace_recursive ( $data [0], $d_out );
}
}
}
return $data;
}
调用
$data = $this->build ( $data, array (
"exe" => array (
"replaceImg" => array (
"param" => array (
array("preUrl"=>"http://www.xx.com/data/uploads/","fild"=>"type_data.thumburl"),
array("preUrl"=>"http://www.xx.com/data/uploads/","fild"=>"type_data.thumbhaibaourl")
)
)
),
"url" => $this->URL_ARR
), false );
用到的build方法
public function build($data = array(), $option = array(), $isarray = true) {
if (! empty ( $data ) && is_array ( $data )) {
// @todo需要加一个try...catch
if ($isarray) { // 处理数组
foreach ( $data as $k => $v ) {
if (! empty ( $option ) && is_array ( $option )) {
foreach ( $option ["exe"] as $ok => $ov ) {
$methods = $ok; // 需要执行的方法
$options_build ["url"] = $option ["url"];
$options_build ["param"] = $ov ["param"];
if (! empty ( $ov ["oData"] )) {
$options_build ["oData"] = $ov ["oData"];
}
$data [$k] = $this->$methods ( $data [$k], $options_build );
}
}
}
} else { // 处理一条数据
if (! empty ( $option ) && is_array ( $option )) {
foreach ( $option ["exe"] as $ok => $ov ) {
$methods = $ok; // 需要执行的方法
$options_build ["url"] = $option ["url"];
$options_build ["param"] = $ov ["param"];
if (! empty ( $ov ["oData"] )) {
$options_build ["oData"] = $ov ["oData"];
}
$data = $this->$methods ( $data, $options_build );
unset ( $options_build );
}
}
}
}
return $data;
}
本例子中,需要根据$data的结构修改replaceImg方法中取值项
本例子中,主要特点是能替换多维数组中的内容
$data=array("type_data"=>array("thumbhaibaourl"=>"aaa.png"))
这样写
array("preUrl"=>"http://www.xx.com/data/uploads/","fild"=>"type_data.thumbhaibaourl")
就可以把$data中的aaa.png替换为http://www.xx.com/data/uploads/aaa.png
如果数组在多几级,只要键值type_data.thumbhaibaourl.aaa.bbb.ccc对应,就可以替换啦