仅当结果中存在数组时,如何回显str_replace

时间:2015-06-03 05:38:10

标签: php foreach echo str-replace

对于PHP来说,我非常业余,所以希望这是有道理的。我有以下foreach循环,可以获取所有"信用"与使用该艺术家的id(混音师,制作人,编曲家,作曲家等)的音乐艺术家相关联。我希望能够用更多的东西替换不同的信用名称" 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语句,以便它只替换&#34;数组&#34;结果是什么?

5 个答案:

答案 0 :(得分:1)

添加检查$skill['credit_name']是否为数组 -

if(is_array($skill['credit_name'])) {
    // Your code
}

答案 1 :(得分:0)

您可以使用is_array功能和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']);
}

答案 2 :(得分:0)

除了评估此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?>

答案 3 :(得分:0)

感谢大家的回复,我摆弄了所发布的建议,并意识到我没有在第一个数组中指定足够的原始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; ?>

答案 4 :(得分:0)

你可以通过这样做来实现这一点,

$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包含已替换的字符串。