PHP 在函数中循环遍历数组和更改数组内的值的问题,代码在函数外工作


PHP problems with looping through array in function and changing values inside array, code works outside function

我正在尝试编写一段代码:

  1. 转换包含人员列表的字符串,$currentAuthors ,到一个数组中,$authors每个人都是一个值数组。
  2. 将该数组中的每个人
  3. 与另一个数组中的每个人进行比较,如果它们匹配,则将值$authors包装在<a href="#"></a>(为了举例说明)。
  4. 从此数组创建另一个字符串,使用逗号 "、" 和 " 和 " 分隔符,以便字符串读取良好。

基本上,它将某些人的名字变成链接。

代码在这里,这有效:

$currentAuthors = "Paul Peters,  Joe Bloggs,  Chloe Brown, Sarah Smith, Anna Smith and Mark Jones";
$linkableAuthors = array("Joe Bloggs","Anna Smith");
echo "Initial string: ".$currentAuthors."<br />";
// replace all instances of "and" with "," so all authors in $newAauthors are separated by a comma
$replaceAnds = str_replace(" and ", ",", $currentAuthors);
//create array from $currentAuthors, using comma as delimiter
$authors = explode(",", $replaceAnds );
$authorsCount = count($authors);
foreach ($authors as $key=>&$author) {
    // Trim spaces from beginning and end of each author, if there are any
    $author = trim($author);
    foreach ($linkableAuthors as $linkableAuthor) {
        // check if each value in $authors matches any of the $linkableAuthors, if so, make it a link.
        if ($author == $linkableAuthor) {
            $author = "<a href='#'>".$author."</a>";
        }
    }
}
$fullAuthorList = "";
// logic for recreating initial string with new links added in; creating separators
foreach ($authors as $key=>$authorFinal) {
    $fullAuthorList .= $authorFinal;
    if ($authorsCount==1) {
        // do nothing, only one author
    }
    elseif ($key==$authorsCount-1) {
        // do nothing, on last author
    }
    elseif ($key==$authorsCount-2) {
        $fullAuthorList .= " and ";
    }
    else {
        $fullAuthorList .= ", ";
    }
}
$fullAuthorList .= "</p>";
echo "Final string: ".$fullAuthorList;

结果是:


初始字符串:Paul Peters、Joe Bloggs、Chloe Brown、Sarah Smith、Anna Smith 和 Mark Jones

最后一串:保罗·彼得斯、乔·博格斯、克洛伊·布朗、莎拉·史密斯、安娜·史密斯和马克·琼斯


但是,如果我将此代码转换为函数,则不再添加Joe Bloggs和Anna Smith的链接。知道为什么吗?必须是与$authors中的值未正确更改有关的内容。

代码在这里不起作用:

$currentAuthors = "Paul Peters,  Joe Bloggs,  Chloe Brown, Sarah Smith, Anna Smith and Mark Jones";
$linkableAuthors = array("Joe Bloggs","Anna Smith");
function getAuthors ($input) {
    echo "Initial string: ".$input."<br />";
    // replace all instances of "and" with "," so all authors in $newAauthors are separated by a comma
    $replaceAnds = str_replace(" and ", ",", $input);
    //create array from $currentAuthors, using comma as delimiter
    $authors = explode(",", $replaceAnds );
    $authorsCount = count($authors);
    foreach ($authors as $key=>&$author) {
        // Trim spaces from beginning and end of each author, if there are any
        $author = trim($author);
        foreach ($linkableAuthors as $linkableAuthor) {
        // check if each value in $authors matches any of the $linkableAuthors, if so, make it a link.
            if ($author == $linkableAuthor) {
                $author = "<a href='#'>".$author."</a>";
            }
        }
    }
    $fullAuthorList = "";
    // logic for recreating initial string with new links added in; creating separators
    foreach ($authors as $key=>$authorFinal) {
    $fullAuthorList .= $authorFinal;
    if ($authorsCount==1) {
        // do nothing, only one author
    }
    elseif ($key==$authorsCount-1) {
        // do nothing, on last author
    }
    elseif ($key==$authorsCount-2) {
        $fullAuthorList .= " and ";
    }
    else {
        $fullAuthorList .= ", ";
    }
    }
    $fullAuthorList .= "</p>";
    echo "Final string: ".$fullAuthorList;
}
getAuthors($currentAuthors);

结果这次没有添加链接:


初始字符串:Paul Peters、Joe Bloggs、Chloe Brown、Sarah Smith、Anna Smith 和 Mark Jones

最后一串:保罗·彼得斯、乔·博格斯、克洛伊·布朗、莎拉·史密斯、安娜·史密斯和马克·琼斯


非常感谢!我是学习PHP的新手。

发生这种情况是因为 $linkableAuthors 变量对该函数不可用。您需要在函数中将其声明为全局。换句话说:

function getAuthors ($input) {
    global $linkableAuthors;
    echo "Initial string: ".$input."<br />";
[...]

有关变量作用域的详细信息,请参阅手册。