Handling array return

Hi there,

So I need to return two strings from a function, so I decided to place them into an array and then return the array.

Now I want to echo the first element of the array, but I’m having trouble selecting it.

Here’s my code so far:


function download2html($text)
{
  $text = html($text);

  //[DOWNLOAD=file]link[/DOWNLOAD]
  $text = preg_replace('/\\[DOWNLOAD=([-a-z0-9._~:\\/?#@!$&\\'( )*+,;=%]+)](.+?)\\[\\/DOWNLOAD]/i', '<a href="download.php"><img src="/images/download.png" /></a>', $text);
  
  $file = '$1';
  $result = array($text, $file);
  
  return $result;
}

function downloadout($text)
{
  [b]echo (download2html($text)[0]);[/b]
}

Obviously if I echo download2html($text) I get “Array”. If I print_r(download2html($text), I get “Array([0] => <a href=“download.php”><img src=”/images/download.png" /></a> [1] => $1)".

I’m getting caught out with syntax I think. Or am I going about this the wrong way?

Many thanks,
Mike

You cannot reference elements of an array returned by a function; it’s not valid syntax.


echo returnArray()[0]; // invalid

$result = returnArray();
echo $result[0]; // valid

Patch for this has been committed to PHP repo (as “array dereferencing”) and hopefully the feature will be available in a future version of PHP.