PHP – Replacing A Value Inside An Array Or Multi Array

This simple php script find replaces all values find in an array including the ‘key’ values.

	function changedValueOfAnArray($arr = null, $from = null, $to = null){
	    $newArr = array();
	    if (is_array($arr)){
    	    foreach ($arr as $k => $v){
    	        $newKey = $k;
    	        if (!strpos($from, $k)){
    	            $newKey = str_replace($from, $to, $k); //$to;
    	        }
    	        $newV = $v;
	            if (is_array($v)){
	                $newV = changedValueOfAnArray($v, $from, $to);
	            }
	            else{
	                if (!empty($from) && !empty($v)){
    	                if (!strpos($from, $v)){
    	                    $newV = str_replace($from, $to, $v);
    	                }
	                }
	            }
	            $newArr[$newKey] = $newV;
    	    }
	    }
	    return $newArr;
	}

Tags:

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.