algalg
algalg

Reputation: 821

PHP: How to find and replace values for a key with random text from a list in a varying-length array

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);

Upvotes: 1

Views: 124

Answers (2)

cam8001
cam8001

Reputation: 1641

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);
}

Upvotes: 2

atymic
atymic

Reputation: 3128

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);

Upvotes: 1

Related Questions