2017-09-13 72 views
0

我有數據表,我需要在每次迭代中設置標題。該數組包含所有標題。在每次迭代中追加數組值

這個工作正常

$this->table->set_heading("","$name[0]","$name[1]","$name[2]","$name[3]","$name[4]","$name[5]"); 

,但我需要動態設置

$cnt=count(name); 
for($i=0;$i<$cnt;$i++) 
{ 
     //$this->table->set_heading($name[$i],);  
} 

的任何解決方案,解決了這個

+0

您可以簡單地使用'$這個 - >表 - > set_heading( 「」,$名));' –

回答

0

你調用的函數接受一個數組,但是對於做功能不是你可以使用下面的技術。可以使用call_user_func_array

$args = [""]; 
$cnt = count($name); 
for($i = 0; $i < $cnt; ++$i) 
    $args[] = $name[$i]; 
call_user_func_array([$this->table, 'set_heading'], $args); 

循環可能是一個更好一點太多,如果你真的不關心指數,即:

$args = [""]; 
foreach($name as $arg) 
    $args[] = $arg; 
call_user_func_array([$this->table, 'set_heading'], $args); 

或者,你可以只複製陣列和第一個參數轉嫁到陣列

$args = $name; 
array_unshift($args, ""); 
call_user_func_array([$this->table, 'set_heading'], $args); 

也請注意,你的周圍用雙引號(即:"$name[0]")參數,同時在技術上是正確的,並會工作,是拙劣的形式,降低性能,只需要使用他們迪直截了當地沒有雙引號。

0

如果$ name是標題字符串數組,例如

$name = array("first", "last", "address", "phone"); 

然後,所有你需要做的是

$this->table->set_heading($name);