-3

Using an answer found here. I am trying to randomly select a URL found for a PHP variable called $url.

Here is the current code:

$url1 = "https://www.domain1.com/tds/parser.php?station=".$_GET['id'];
$url2 = "https://www.domain2.com/tds/parser.php?station=".$_GET['id'];

$vals = array($url1,$url2);

$url = array($vals[array_rand($vals, 1)]);

echo $url;

My echo statement returns just the words 'array' instead of the actual url randomly selected?

9
  • 1
    var_dump( $url ); will reveal your issue. Commented Dec 26, 2018 at 19:36
  • You didn't select a URL, you just shuffled the array. Now you can use echo $url[0] Commented Dec 26, 2018 at 19:36
  • 3
    $url = array($vals[array_rand($vals, 1)]); is confusing and probably unnecessary. Just shuffle($vals); and echo $vals[0]; Commented Dec 26, 2018 at 19:37
  • your shuffle is useless because at this point, your array.length === 1 Commented Dec 26, 2018 at 19:37
  • I don't want to display the url with an echo, I want to make that variable available for later in the program after it's randomly shuffled. Commented Dec 26, 2018 at 19:38

1 Answer 1

1

The problem is with this line:
$url = array($vals[array_rand($vals, 1)]);

This will assign an array to the variable $url

Change it to $url = $vals[array_rand($vals, 1)];

<?php

$vals = array(
    "https://www.domain1.com/tds/parser.php?station=".$_GET['id'],
    "https://www.domain2.com/tds/parser.php?station=".$_GET['id']
);

$url = $vals[array_rand($vals, 1)];

echo $url;
Sign up to request clarification or add additional context in comments.

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.