I see this thread got hijacked a bit.
But i have still not found a solution to my problem.
I've changed things abit now, most likely this cant be done with referencing so i've skipped that and just works on a copy instead and letting the function return a modified array that is correct.
This function now returns a complete copy of the array given to it. What i need now is to figure out how to change the value i want to change.
It feels like im codeblinded with this problem now, im sure its quite simple but i just cant see what is wrong.
Some revision would be nice.
(As you can see im building up a copy of the original array with the change-value replaced.)
PHP Code:
// Path is a path in the array. I think its quite self explaining what i want to change
$path = array("info", "data");
// This is the original array
$test = array
(
"id" => 1,
"name" => "testing",
"info" => array
(
"data" => "some data here yes"
)
);
// This is how far i got with a function to deal with this
function arrayValueReplace($path, &$arr, $replace) {
// Create a copy of the array to work on
$array = $arr;
foreach ($array as $key => $ar) {
// This is where we are in the path
$current = array_slice($path, 0, 1);
// Some recursivness
if (is_array($ar[$current])) {
$build[$key] = arrayValueReplace($path, $ar[$current], $replace);
}
else {
// This is were i need to figure out if we are on the right spot and to change the value with the replacevalue
if ($ar == $current[0] && count($path) == 1) {
$build[$key] = $replace;
}
else {
$build[$key] = $ar;
}
}
// Remove the portion of path we are done with
$path = array_slice($path, 1);
}
// Return the copy we are building up
return $build;
}
// In order to test the restults:
echo "This is the original array: <pre>";
print_r($test);
echo "</pre> This is the final array which should have the correct value changed: <pre>";
print_r(arrayValueReplace($path, $test, "Nice new text"));
echo "</pre>";
This returns two equal arrays. So i have succesfully rebuilt the array, but not changed the value.
Bookmarks