0

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>';
4
  • 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. Commented Sep 21, 2010 at 18:30
  • it doesn't. i still see $status and $title in the url field :( Commented Sep 21, 2010 at 18:31
  • 2
    You have to use double quotes in the array. Commented Sep 21, 2010 at 18:36
  • @JMC: Good eye. I missed that. That alone won't fix it, but the correct answera are below alredy. Commented Sep 21, 2010 at 18:37

6 Answers 6

4

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}",
                 ....
                );
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

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

1 Comment

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

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

Comments

1

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.

Comments

0

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

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.