1

I have a varying array for a playlist, containing media/source URLs for each item. Like this:

$playlist = array(
  array(
    "title" => "something",
    "sources" => array(
      array(
        "file" => "https://url.somedomain.com/path/file1.mp3"
      )
    ), 
    "description" => "somedesc",
    "image" => "http://imagepath/",
    "file" => "https://url.somedomain.com/path/file1.mp3"
  ),
  array(
    "title" => "elsewaa", 
    "sources" => array(
      array(
        "file" => "https://url.somedomain.com/someother/file2.mp3"
      )
    ), 
    "description" => "anotherdesc", 
    "image" => "http://someotherimagepath/", 
    "file" => "https://url.somedomain.com/someother/file2.mp3"
  )
);

How do I find and replace the values in the file keys to 'randomise' the choice of subdomain?

For example, if the file key contains url.foo.com, how do I replace the url.foo.com portion of the array value with either differentsubdomain.foo.com or anotherplace.foo.com or someotherplace.foo.com?

I was kindly offered a solution for a single string in this question/answer that used str_replace (thanks Qirel!), but I need a solution that tackles the above array configuration specifically.

All the nesting in the array does my head in!

Is it possible to adapt Qirel's suggestion somehow?

$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);
2
  • So, just to be correct, you want to replace the subdomain in file key with a random one from a list? (ignoring the sources array) Commented Jul 28, 2019 at 2:01
  • I need to change the values nested in sources and also the file key on its own at the end. Basically check and replace any/all references in file that match url.foo.com. Commented Jul 28, 2019 at 2:05

2 Answers 2

2

If you are just asking how to access members in nested arrays, I think you want this:

$random_values = ['differentsubdomain.foo.com', 'anotherplace.foo.com', 'someotherplace.foo.com'];

// Iterate through the array, altering the items by reference.
foreach ($playlist as &$item) {
   $random_key = array_rand($random_values);
   $new_domain = $random_values[$random_key];
   $item['file'] = str_replace('url.foo.com', $new_domain);
   $item['sources'][0]['file'] = str_replace('url.foo.com', $new_domain);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here's an example using recursion to replace the subdomains in any keys named file with a random one.

function replaceUrlHost(&$array, $hostDomain, $subdomains)
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $array[$key] = replaceUrlHost($value, $hostDomain, $subdomains);
            continue;
        }

        if ($key !== 'file') {
            continue;
        }

        $hostname = parse_url($value, PHP_URL_HOST);

        if (strpos($hostname, $hostDomain) === false) {
            continue;
        }

        $array[$key] = str_replace(
            $hostname,
            $subdomains[array_rand($subdomains)] . '.' . $hostDomain,
            $value
        );
    }

    return $array;
}

// usage
$subdomains = ['bar', 'baz', 'bing', 'bop'];
$out = replaceUrlHost($playlist, 'somedomain.com', $subdomains);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.