views:

58

answers:

6

Hi

I have a array that looks like this:

  $sites = array('Twitter' => 'http://twitter.com/home?status=$status',
                 'Digg' => 'http://digg.com/submit?phase=2&title=$title',
                 ....
                );

  $status = 'bla bla';
  $title = 'asdasf';

  foreach($sites as $site_name=>$site_url)
    echo '<li><a href="'.$site_url.'">'.$site_name.'</a></li>';

Notice the $status and $title keywords in the array fields. Is there any way I can "map" these keywords to variables I set below?

so the output would be:

<li><a href="http://twitter.com/home?status=bla bla">Twitter</a></li>';
+2  A: 

Why not do this, set the $status and $title first, then append to the array you produce. They are then ready and set ready for when you output the link

$status = 'bla bla';
$title = 'asdasf';

$sites = array('Twitter' => 'http://twitter.com/home?status=' . $status,
    'Digg' => 'http://digg.com/submit?phase=2&amp;amp;title=' . $title,
    ....
    );


foreach($sites as $site_name=>$site_url)
    echo '<li><a href="'.$site_url.'">'.$site_name.'</a></li>';
jakenoble
looks kind of ugly :)
Alex
But it works. I found variables in double-quoted strings to be ugly. You cannot see them easily when skim reading the code, it just looks like a string. This way you can see that the string stops and contains a variable.
jakenoble
+2  A: 

Just assign $status and $title first, then let string interpolation do the work for you when you create the array. It will require a change to double quotes to work. See:

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

LVB
thanks, the " quotes, that was it :)
Alex
+4  A: 

Single quoted strings will not perform variable substitution. Set the variables before the array and use double quotes. I also like to use braces for clarity:

$status = 'bla bla';
$title = 'asdasf';

$sites = array('Twitter' => "http://twitter.com/home?status={$status}",
                 'Digg' => "http://digg.com/submit?phase=2&amp;amp;title={$title}",
                 ....
                );
Matt McCormick
+1  A: 

Wouldn't this work...

$status = 'bla bla';
$title = 'asdasf';

foreach($sites as $site_name=>$site_url){
   echo '<li><a href="'.$site_url.'?status='.$status">'.$site_name.'</a></li>';
}

I'm not sure what you're trying to do with $title

Don
+1  A: 

If you can move the code around:

$status = 'bla bla';
$title = 'asdasf';

$sites = array('Twitter' => "http://twitter.com/home?status=$status",
                 'Digg' => "http://digg.com/submit?phase=2&amp;amp;title=$title",
                 ....
                );

Otherwise:

function get_sites($status, $title)
{
  return array('Twitter' => "http://twitter.com/home?status=$status",
                     'Digg' => "http://digg.com/submit?phase=2&amp;amp;title=$title",
                     ....
                    );
}

$sites = get_sites('bla blah', 'asdasf');

As another alternative:

$sites = array('Twitter' => 'http://twitter.com/home?status=$status',
                 'Digg' => 'http://digg.com/submit?phase=2&amp;amp;title=$title',
                 ....
                );

foreach($sites as $site_name=>$site_url)
{
  $site_url = strtr($site_url, array('$status' => 'bla blah', '$title' => 'asdasf'));
  echo '<li><a href="'.$site_url.'">'.$site_name.'</a></li>';
}

I wouldn't recommend the last approach unless there's a lot of arbitrary content to change.

The first is the best if it works for you.

konforce
A: 

Use nested sprintf if you want to define $status after the $sites declaration:

<?php
// $sites is defined in a bootstrap / settings file ....
$sites = array(
    'Twitter' => 'http://twitter.com/home?status=%s',
    'Digg' => 'http://digg.com/submit?phase=2&amp;title=%s',
);

....

// $status can be dynamic, loaded from a db, etc.
$status = 'omglol';

....

// And output!
foreach ($sites as $name => $url) {
    echo sprintf('<li><a href="%s">%s</a></li>', 
            sprintf($url, $status),
            $name);
}
pygorex1