2013-11-09 38 views
0

我試圖寫一段代碼:PHP問題,通過陣列功能循環和改變內部數組值,代碼工作之外的功能

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

結果是沒有鏈接添加了這個時間:


初始字符串:保羅·彼得斯,李四,克洛伊布朗,薩拉·史密斯,安娜·史密斯和馬克· Jones

最終字符串:Paul Peters,Joe Bloggs,Chloe Brown,Sarah Smith,Anna Smith和Mark Jones


非常感謝!我是學習PHP的新手。

回答

1

這是因爲$linkableAuthors變量不可用於該功能。你需要在函數中聲明它爲全局的。換句話說:

function getAuthors ($input) { 

    global $linkableAuthors; 

    echo "Initial string: ".$input."<br />"; 

[...] 

有關變量範圍的更多信息,請參見the manual

+0

太好了,非常感謝! – Sarah