dounai6613 2013-11-09 17:28
浏览 80
已采纳

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

I am trying to write a piece of code that:

  1. converts a string containing a list of people, $currentAuthors, into an array $authors with each person being a value in that array.
  2. compares each person in that array with each person in another array, and if they match, wraps the value in $authors inside <a href="#"> and </a> (for examples sake).
  3. create another string from this array, using commas ", " and " and " separators so that the string reads well.

Basically, it makes certain people's names into links.

Code is here and this works:

$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;

Result is:


Initial string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones

Final string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones


However, if I make this code into a function the links for Joe Bloggs and Anna Smith are no longer added. Any ideas why? Must be something to with the values in $authors not being changed properly.

Code not working here:

$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);

Result is without links added this time:


Initial string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones

Final string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones


Thanks very much! I'm new to learning PHP.

  • 写回答

1条回答 默认 最新

  • doulian8742 2013-11-09 17:39
    关注

    It's happening because the $linkableAuthors variable is not available to the function. You need to declare it as a global inside the function. In other words:

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

    For more information on variable scope, see the manual.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?