2014-03-06 18 views
2

我有一個包含多個其他數組的數組。這些數組有一個名爲[note]的值,它引用一個名爲$theNote的變量,它位於數組的上方/外部。這個變量包含一個簡單的模板,只有幾個跨度。PHP - 使用數組之外的值

$theNote = '<span class="icon"></span><span>Hello my name is $thename</span>'; 

我有一個名爲client_infos數組,它包含內部像多個陣列

'client_infos' => array (
     array (
      'name' => 'John smith', 
      'note' => $theNote, 
      'prepend' => '', 
      'append' => '', 
      'formatting' => 'html', 
     ), 
     array (
      'name' => 'Mary smith', 
      'note' => $theNote, 
      'prepend' => '', 
      'append' => '', 
      'formatting' => 'html', 
     ), 
    ); 

將有數目不詳的名字最終。我需要能夠做的就是在我的模板,調用在一個循環或東西$theNote及以下將被輸出......

<span class="icon"></span><span>Hello my name is John Smith</span> 

正如你所看到的,在陣列使用的【注意事項】信息$theNote變量,這將輸出一個代碼塊,其中包含一個名爲$thename的變量。我不知道如何讓我的[姓名]信息進入$thename變量。

基本上有些如何得到。 $thename裏面$theNote to = the current array [name] value

所有這一切的原因是,我可以輕鬆地更新「筆記」代碼一次,而無需在所有子數組中完成。

我希望我足夠清楚,有什麼想法?

+0

此外,任何幫助實際命名這個問題更好? – bruffstar

+0

所以你想循環通過client_infos輸出每個['名稱']?是嗎? – jan

回答

0

如果我有問題的權利,你可以像%THE_NAME%了獨特的可變更換$theName,做這樣的事情:

$theNote = '<span class="icon"></span><span>Hello my name is %THE_NAME%</span>'; 

$client_infos = array (
     array (
      'name' => 'John smith', 
      'note' => $theNote, 
      'prepend' => '', 
      'append' => '', 
      'formatting' => 'html', 
     ), 
     array (
      'name' => 'Mary smith', 
      'note' => $theNote, 
      'prepend' => '', 
      'append' => '', 
      'formatting' => 'html', 
     ), 
    ); 

foreach($client_infos as $arr) 
{ 
    echo str_replace('%THE_NAME%', $arr['name'], $arr['note']) , '<br/>'; 
} 

輸出

Hello my name is John smith 
Hello my name is Mary smith 
0

事情是這樣的:

 $theNote = '<span class="icon"></span><span>Hello my name is {name}</span>'; 

然後回顯:

echo str_replace('{name}', $client_infos[0]['name'], $client_infos[0]['note']); 

或者替換{name}的所有陣列

for($i=0; $i<count($client_infos); $i++){ 
     $client_infos[$i]['note']=str_replace('{name}', $client_infos[$i]['name'], $client_infos[$i]['note']); 
    } 
3

這可能是更好的使用這裏的類:

class Client 
{ 
    private $name; 
    private $prepend; 
    private $append; 
    private $formatting; 

    // place a constructor here 

    // create getter and setter for the properties 
    public function getNote() { 
     return '<span class="icon"></span><span>Hello my name is ' . $this->name . '</span>' 
    } 
} 
+0

在這種情況下考慮面向對象的編程會很好,在這種情況下,你可以用類對象而不是數組來描述客戶端。 – rsakhale

+0

使用類並不總是更好,特別是當像這樣的過程可以用簡單的'str_replace'完成時 – Peon

-1

嘗試陣列本身內聲明的$theNote部分:

$theNote = '<span class="icon"></span><span>Hello my name is '; 
$theNote2 = '</span>'; 
'client_infos' => array (
     array (
      'name' => 'John smith', 
      'note' => $theNote . $thename . $theNote2, 
      'prepend' => '', 
      'append' => '', 
      'formatting' => 'html', 
     ), 
     array (
      'name' => 'Mary smith', 
      'note' => $theNote . $thename . $theNote2, 
      'prepend' => '', 
      'append' => '', 
      'formatting' => 'html', 
     ), 
    ); 

祝你好運!