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>';
link|improve this question

What's wrong with the code you have? Looks like it should work. I'm not sure if you can omit {} braces after foreach, but otherwise looks fine. – Mchl Sep 21 '10 at 18:30
it doesn't. i still see $status and $title in the url field :( – Alex Sep 21 '10 at 18:31
2  
You have to use double quotes in the array. – JMC Sep 21 '10 at 18:36
@JMC: Good eye. I missed that. That alone won't fix it, but the correct answera are below alredy. – Mchl Sep 21 '10 at 18:37
feedback

6 Answers

up vote 2 down vote accepted

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

link|improve this answer
thanks, the " quotes, that was it :) – Alex Sep 21 '10 at 18:35
feedback

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;title={$title}",
                 ....
                );
link|improve this answer
feedback

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;title=' . $title,
    ....
    );


foreach($sites as $site_name=>$site_url)
    echo '<li><a href="'.$site_url.'">'.$site_name.'</a></li>';
link|improve this answer
looks kind of ugly :) – Alex Sep 21 '10 at 18:35
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 Sep 21 '10 at 18:37
feedback

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

link|improve this answer
feedback

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

link|improve this answer
feedback

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&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);
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.