IM使用下面的代码输出数组如何把一个变量在引号
$values = array();
foreach ($album as $a){
$values[] = $a['value'];
}
$string = implode(' or ', $values);
}
返回
1 or 2 or 3
现在我怎样才能把“” arouns每个值,所以看起来像
"1" or "2" or "3"
感谢所有帮助
IM使用下面的代码输出数组如何把一个变量在引号
$values = array();
foreach ($album as $a){
$values[] = $a['value'];
}
$string = implode(' or ', $values);
}
返回
1 or 2 or 3
现在我怎样才能把“” arouns每个值,所以看起来像
"1" or "2" or "3"
感谢所有帮助
if (!empty($values)) {
$string = '"' . implode('" or "', $values) . '"';
} else {
$string = 'What do you think you\'re doing!?';
}
这只会引用整个事情,而不是不同的阵列部分。 –
@Alex,OP想要的字符串输出,而不是数组。 – 65Fbef05
不是我的意思,但我只是注意到“或”的引号,所以我错了,这将起作用。 –
这在我看来是比较容易:
$values = array();
foreach ($album as $a){
$values[] = '"'.$a['value'].'"'; //concat quotes on each side of the value
}
$string = implode(' or ', $values);
}
这里有一个干净的解决方案,将与空数组正常工作:
$string = implode(' or ', array_map(function($value) {
return '"' . $value . '"';
}, $values));
演示(从php -a
外壳复制):
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 >
可能是最简洁的解决方案,但也是最难读的。 –
那么,这个例子很难阅读。代码本身非常易读 – ThiefMaster
你尝试了什么?它有用吗?如果不是,为什么不呢?我不会downvote(主要是由于-3已经),但只是做它;-) – 2012-06-26 20:32:40