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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!