仅当结果中存在数组时如何回显 str_replace
How to echo an str_replace only if array exists in results
我在 PHP 方面非常业余,所以希望这是有道理的。我有以下 foreach 循环,它使用艺术家的 id(混音器、制作人、编曲者、作曲家等)获取链接到音乐艺术家的所有 "credits"。我希望能够用更多的东西替换不同的信用名称 "list friendly." 下面的尝试有效,但它只会循环,直到它遇到与艺术家无关的信用,然后它停止。
<?php function getSkills($id)
{
$query = "SELECT c2a.credit_id, cr.credit_name
FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.artist_id = $id
GROUP BY c2a.credit_id
ORDER BY cr.credit_name";
$res = mysql_query($query);
while ($row = mysql_fetch_assoc($res)) {
$skills[] = $row;
}
return $skills;
}
?>
<?php foreach (getSkills($id) as $skill): ?>
<?php echo str_replace(
array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer'),
array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance'),
$skill['credit_name']); ?><br />
<?php endforeach; ?>
如何将 if 语句添加到代码中,以便它仅替换结果中的 "array"?
添加检查 $skill['credit_name']
是否为数组 -
if(is_array($skill['credit_name'])) {
// Your code
}
您可以使用 is_array
function along with count
作为
if(is_array($skill['credit_name']) && count($skill['credit_name']) > 0){
echo str_replace(array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer'), array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance'), $skill['credit_name']);
}
除了在 foreach()
中评估技能外,你没有做任何事情,你可以将整个块放在外面,如下所示,假设 $skill['credit_name']
是字符串,
<?php if(is_array(getSkills($id)): ?>
<?php foreach (getSkills($id) as $skill): ?>
<?php if(is_string($skill['credit_name']) && $skill['credit_name']!=''): ?>
<?php echo str_replace(
array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer'),
array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance'),
$skill['credit_name']); ?><br />
<?php endif?>
<?php endforeach?>
<?php endif?>
感谢大家的回复,我摆弄了张贴的建议,意识到我没有在第一个数组中指定足够多的原始 credit_name
来匹配第二个数组。而且它也必须是一个字符串,因为 if(is_string)
有效而 if(is_array)
无效。 (正如我所说,所有这些仍然非常环保!)
<?php foreach (getSkills($id) as $skill): ?>
<?php if(is_string($skill['credit_name'])) {
echo str_replace(
array('arranger','mixer','producer','composer','engineer','recorder','vocalist','writer'),
array('song arrangement','audio mixing','music production','song composition','music engineering','music/vocal recording','singing/performance','song writing'),
$skill['credit_name']);
echo "<br />";
} ?>
<?php endforeach; ?>
你可以这样做,
$result = getSkills($id);
$search = array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer');
$replace = array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance');
foreach($result as &$v)
{
$v['credit_name'] = str_replace($search,$replace,$v['credit_name']);
}
现在 $result
包含替换的字符串。
我在 PHP 方面非常业余,所以希望这是有道理的。我有以下 foreach 循环,它使用艺术家的 id(混音器、制作人、编曲者、作曲家等)获取链接到音乐艺术家的所有 "credits"。我希望能够用更多的东西替换不同的信用名称 "list friendly." 下面的尝试有效,但它只会循环,直到它遇到与艺术家无关的信用,然后它停止。
<?php function getSkills($id)
{
$query = "SELECT c2a.credit_id, cr.credit_name
FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.artist_id = $id
GROUP BY c2a.credit_id
ORDER BY cr.credit_name";
$res = mysql_query($query);
while ($row = mysql_fetch_assoc($res)) {
$skills[] = $row;
}
return $skills;
}
?>
<?php foreach (getSkills($id) as $skill): ?>
<?php echo str_replace(
array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer'),
array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance'),
$skill['credit_name']); ?><br />
<?php endforeach; ?>
如何将 if 语句添加到代码中,以便它仅替换结果中的 "array"?
添加检查 $skill['credit_name']
是否为数组 -
if(is_array($skill['credit_name'])) {
// Your code
}
您可以使用 is_array
function along with count
作为
if(is_array($skill['credit_name']) && count($skill['credit_name']) > 0){
echo str_replace(array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer'), array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance'), $skill['credit_name']);
}
除了在 foreach()
中评估技能外,你没有做任何事情,你可以将整个块放在外面,如下所示,假设 $skill['credit_name']
是字符串,
<?php if(is_array(getSkills($id)): ?>
<?php foreach (getSkills($id) as $skill): ?>
<?php if(is_string($skill['credit_name']) && $skill['credit_name']!=''): ?>
<?php echo str_replace(
array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer'),
array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance'),
$skill['credit_name']); ?><br />
<?php endif?>
<?php endforeach?>
<?php endif?>
感谢大家的回复,我摆弄了张贴的建议,意识到我没有在第一个数组中指定足够多的原始 credit_name
来匹配第二个数组。而且它也必须是一个字符串,因为 if(is_string)
有效而 if(is_array)
无效。 (正如我所说,所有这些仍然非常环保!)
<?php foreach (getSkills($id) as $skill): ?>
<?php if(is_string($skill['credit_name'])) {
echo str_replace(
array('arranger','mixer','producer','composer','engineer','recorder','vocalist','writer'),
array('song arrangement','audio mixing','music production','song composition','music engineering','music/vocal recording','singing/performance','song writing'),
$skill['credit_name']);
echo "<br />";
} ?>
<?php endforeach; ?>
你可以这样做,
$result = getSkills($id);
$search = array('arranger','mixer','producer','composer','engineer','mixer','recorder','vocalist','writer');
$replace = array('song arrangement','audio mixing','music production','music/vocal recording','song writing','singing/performance');
foreach($result as &$v)
{
$v['credit_name'] = str_replace($search,$replace,$v['credit_name']);
}
现在 $result
包含替换的字符串。