title: PHP: How to find and replace values for a key with random text from a list in a varying-length array
body: <p>I have a varying array for a playlist, containing media/source URLs for each item. Like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$playlist = array(
    array(
      "title" =&gt; "something",
      "sources" =&gt; array(
        array(
          "file" =&gt; "https://url.somedomain.com/path/file1.mp3"
        )
      ), 
      "description" =&gt; "somedesc",
      "image" =&gt; "http://imagepath/",
      "file" =&gt; "https://url.somedomain.com/path/file1.mp3"
    ),
    array(
      "title" =&gt; "elsewaa", 
      "sources" =&gt; array(
        array(
          "file" =&gt; "https://url.somedomain.com/someother/file2.mp3"
        )
      ), 
      "description" =&gt; "anotherdesc", 
      "image" =&gt; "http://someotherimagepath/", 
      "file" =&gt; "https://url.somedomain.com/someother/file2.mp3"
    )
  );
  </code></pre>
  
  <p>How do I find and replace the values in the <code>file</code> keys to 'randomise' the choice of subdomain?</p>
  
  <p>For example, if the <code>file</code> key contains <code>url.foo.com</code>, how do I replace the <code>url.foo.com</code> portion of the array value with either <code>differentsubdomain.foo.com</code> or <code>anotherplace.foo.com</code> or <code>someotherplace.foo.com</code>?</p>
  
  <p>I was kindly offered a solution for a single string in this <a href="https://stackoverflow.com/questions/57235713/php-how-to-replace-a-segment-of-a-string-with-some-random-text-from-a-list">question/answer</a> that used <code>str_replace</code> (thanks Qirel!), but I need a solution that tackles the above array configuration specifically.</p>
  
  <p>All the nesting in the array does my head in!</p>
  
  <p>Is it possible to adapt Qirel's suggestion somehow?</p>
  
  <pre><code>$random_values = ['differentsubdomain.foo.com', 'anotherplace.foo.com', 'someotherplace.foo.com'];
  $random = $random_values[array_rand($random_values)];
  // str_replace('url.foo.com', $random, $file);
  </code></pre>