/** * main function * @param $find * @param $replace * @param $array * @param bool $t * @param string $for * @param bool $init * @param null $c * @return array */ public function &find_replace($find, $replace, &$array, $t = true, $for = 'value', $init = true, &$c = null) { static $h = array(); //draft static $result = array(); //main result main static $draft_result_paths = array(); //purpose for draft static $paths = array(); //all paths found in array if ($init) { $h = array(); $result = array(); $paths = array(); $draft_result_paths = array(); } $valid_recursive = true; //valid recursive while ($valid_recursive) { if (!$c) { $o =& $array; } else { $o =& $c[1]; } //fetch array remain if (is_array($o)) { foreach ($o as $key => &$v) { if (is_array($v)) { if (isset($v->O)) { $v = $v->O; } $h[] = array($key, &$v); //save this item of array for map location of array if ($c) { //save path & valid for it $path = $c[0] . '/' . $key; //prep path for this key if (!count($paths) && !isset($draft_result_paths[$path])) { $paths[] = $path; $draft_result_paths[$path] = 1; //track path } if (!find_replace_check($paths, $c[0], $key)) { foreach ($paths as $u) { if (false !== strpos($u, $c[0])) { $tt = 1; } } if (!isset($tt) && !isset($draft_result_paths[$path])) { $paths[] = $path; $draft_result_paths[$path] = 1; //track path } } } } /*search for value or key*/ $xpath = $c[0] . '/' . $key; if ($for == 'value' && ($find == 'H_OBJECT' ? 'object' : gettype($find)) == gettype($v) && ($find == 'H_OBJECT' ? true : $v === $find) || $for == 'key' && $find === $key) { if (!count($paths) && !isset($draft_result_paths[$xpath])) { $result[] = array(&$v, $xpath); //if no any array in deep, get a result $draft_result_paths[$xpath] = 1; //save this path of result } foreach ($paths as $path) { if (false !== strpos($path, $xpath) && $path !== $xpath && self::valid_result_path($path, $v, $array, $for)) { if (!isset($draft_result_paths[$path])) { $result[] = array(&$v, $path); $m = 1; $draft_result_paths[$path] = 1; //save this path of result } } } if (!isset($m)) { foreach ($paths as $path) { $w = explode('/', $path); if ($w[count($w) - 1] == $c[0] && self::valid_result_path($path . '/' . $key, $v, $array, $for)) { $new_path = $path . '/' . $key; //get new path if (!isset($draft_result_paths[$new_path])) { $result[] = array(&$v, $new_path); $draft_result_paths[$new_path] = 1; //save this path of result } } if ($w[count($w) - 2] === $c[0] && self::valid_result_path(join($w, '/') . '/' . $key, $v, $array, $for)) { array_pop($w); $new_path = join($w, '/') . '/' . $key; //get new path if (!isset($draft_result_paths[$new_path])) { $result[] = array(&$v, $new_path); $draft_result_paths[$new_path] = 1; //save this path of result } } } } if ($t) { $o[$key] = $find == 'COPY_OF_IT' ? _clone($o[$key]) : $find; } } } } if (count($h) && ($n =& $h[0]) && array_shift($h)) { $c =& $n; if (isset($n->O)) { $c =& $n->O; } //for DRW class instance } else { $valid_recursive = false; } } $jo = array($result, $paths); return $jo; }
function &find_replace($a, $b, &$d, $t = true, $for = 'value', $init = true, &$c = null) { static $h = array(); static $r = array(); static $y = array(); if ($init) { $h = array(); $r = array(); $y = array(); } if (!$c) { $o =& $d; } else { $o =& $c[1]; } if (is_array($o)) { foreach ($o as $i => &$v) { if (is_array($v) || isDrw($v)) { if (isset($v->O)) { $v =& $v->O; } $h[] = array($i, &$v); if ($c) { $path = $c[0] . '/' . $i; if (!count($y)) { $y[] = $path; } if (!find_replace_check($y, $c[0], $i)) { foreach ($y as $u) { if (false !== strpos($u, $c[0])) { $tt = 1; } } if (!isset($tt)) { $y[] = $path; } } } } /*search for value or key*/ $xpath = $c[0] . '/' . $i; if ($for == 'value' && ($a == H_OBJECT ? 'object' : gettype($a)) == gettype($v) && ($a == H_OBJECT ? true : $v === $a) || $for == 'key' && isStr($a) && $a === $i) { if (!count($y)) { $r[] = array(&$v, $xpath); } foreach ($y as $z) { if (false !== strpos($z, $xpath)) { $r[] = array(&$v, $z); $m = 1; } } if (!isset($m)) { foreach ($y as $z) { $w = explode('/', $z); if ($w[count($w) - 1] == $c[0]) { $r[] = array(&$v, $z . '/' . $i); } if ($w[count($w) - 2] == $c[0]) { pop($w); $r[] = array(&$v, join($w, '/') . '/' . $i); } } } if ($t) { $o[$i] = $b == COPY_OF_IT ? _clone($o[$i]) : $b; } } } } if (count($h) && ($n =& $h[0]) && array_shift($h)) { if (isset($n->O)) { $n =& $n->O; } find_replace($a, $b, $d, $t, $for, false, $n); } $jo = array($r, $y); return $jo; }