Sam Healey
Sam Healey

Reputation: 676

how to put a variable in quotes

im using the following code to output an array

$values = array();
foreach ($album as $a){
$values[] = $a['value'];
}

$string = implode(' or ', $values);

}

which returns

 1 or 2 or 3

now how can I put " " arouns each value, so it will look like

 "1" or "2" or "3"

Thanks for any help

Upvotes: 0

Views: 3529

Answers (3)

ThiefMaster
ThiefMaster

Reputation: 318518

Here's a clean solution that will work properly with an empty array:

$string = implode(' or ', array_map(function($value) {
    return '"' . $value . '"';
}, $values));

Demo (copied from the php -a shell):

php > $values = array('foo', 'bar', 'moo');
php >     $string = implode(' or ', array_map(function($value) {
php (         return '"' . $value . '"';
php (     }, $values));
php > echo $string;
"foo" or "bar" or "moo"
php >

Upvotes: 1

Brian Glaz
Brian Glaz

Reputation: 15686

This is easier in my opinion:

$values = array();
foreach ($album as $a){
$values[] = '"'.$a['value'].'"'; //concat quotes on each side of the value
}

$string = implode(' or ', $values);

}

Upvotes: 3

65Fbef05
65Fbef05

Reputation: 4522

if (!empty($values)) {
    $string = '"' . implode('" or "', $values) . '"';
} else {
    $string = 'What do you think you\'re doing!?';
}

Upvotes: 7

Related Questions