I have an array like: 

```
$arr = ["pages","homepage","welcomeText"];
$newWelcomeText = "Hello there";

```

and JSON which looks like:

```
{
  lang: "en",
  something: [0, 1, 33],
  pages: {
    homepage: {
      welcomeText: "Hello!",
      subHeaiding: "have a nice day"
    }
  }
}

```

and I want to find a way how to replace the "welcomeText" with a new value.
I tried something like: 

```
public function findAndReplace ($path, $obj, $data, $index = 0) {
        if($index + 1 == sizeof($path)) {
            if(!is_array($obj)) {
                $obj = json_decode(json_encode($obj), true);
            }
            $obj[$path[$index]] = $data;
            return $obj;
        }
        return $this->findAndReplace($path, $obj, $data, $index + 1);
    }

```

I never know how the path will look like so I need some kind of function that receives an array and this object as a parameter and returns a modified object.