【问题标题】:PHP problems with looping through array in function and changing values inside array, code works outside functionPHP在函数中循环遍历数组并更改数组内的值的问题,代码在函数外工作
【发布时间】:2013-11-21 16:34:28
【问题描述】:

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

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

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

代码在这里,这有效:

$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

最后一个字符串: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

最后的字符串:Paul Peters、Joe Bloggs、Chloe Brown、Sarah Smith、Anna Smith 和 Mark Jones


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

【问题讨论】:

    标签: php arrays string if-statement foreach


    【解决方案1】:

    这是因为 $linkableAuthors 变量对函数不可用。您需要在函数内将其声明为全局。换句话说:

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

    有关变量范围的更多信息,请参阅the manual。

    【讨论】:

      猜你喜欢
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多