2013-09-28 105 views
0

下面的工作代碼的對象中:一個foreach使用數組

 $openchart->type("BarChart"); 
    $openchart->options(array('title' => "Opens Stats")); 
    $openchart->columns(array(
     'Date' => array(
      'type' => 'string', 
      'label' => 'Date' 
     ), 
     'yahoo.com' => array(
      'type' => 'number', 
      'label' => 'yahoo.com' 
     ) 
    )); 

    $openchart->addRow(array('Date' => "Today", 'yahoo.com'=> $chart['yahoo.com']['opentoday'])); 

什麼,我希望能夠做的是像下面,但不知道如何完成。

 $openchart->type("BarChart"); 
    $openchart->options(array('title' => "Opens Stats")); 
    $openchart->columns(array(
     'Date' => array(
      'type' => 'string', 
      'label' => 'Date' 
     ), 
foreach($chart[$name]) 
     '$name' => array(
      'type' => 'number', 
      'label' => '$name' 
     ) 
    )); 

    $openchart->addRow(array('Date' => "Today", '$name'=> $chart['$name']['opentoday'])); 

這是可能的嗎? PHP仍然很新。我也使用CakePHP(與學習只是試錯一噸!)

本來我只是想

$openchart->columnts(array(   
      '$name' => array(
      'type' => 'number', 
      'label' => '$name' 
     )); 

而是覆蓋了陣列。也檢出了array_merge,但那是行不通的,不知道爲什麼。

所以現在我的第一次的foreach(未顯示)的結束,我有所有我想要的值的數組,在$圖[$名稱]

並推斷這可能是最好的路線,然後做'列'一次。

愛這個網站存在,得到如此多的成就。謝謝!

+0

只是一對夫婦的指針...把'$ variables'內''$ single_quotes''將有效地使可變文本字符串。改用'「$ double_quotes」'代替。另外,對於你的'foreach'循環,語法是不正確的:查看[本頁](http://php.net/manual/en/control-structures.foreach.php)瞭解更多信息。最後,它看起來並不像'$ chart'或'$ name'在通過'foreach'運行之前已經被定義,這意味着PHP正在對自己說:'對於每個_what_?_ $ chart_?沒有人告訴我什麼'$ chart'的意思是,所以我不知道該怎麼做。「 – jerdiggity

+0

是在發佈後意識到該模擬是該語句錯誤的變量。 ....但除此之外,是我想做的可能w /當前的PHP?爲簡單起見,我沒有包含$ chart或$ name。 [只是想知道是否有可能做一個對象內的foreach類型聲明(希望我使用正確的術語......通過愚弄w/code來學習數組和對象)。閱讀2學習的速度慢,編碼不夠閱讀比例。)] – brizz

回答

0

這是你想知道的嗎?

class test{ 
    public $array = array("foo", "bar"); 

    public function __construct(){} 
} 

$test = new test(); 

foreach($test->array as $key=>$value){ 
    echo "$key=>$value\n"; 
} 

輸出0=>foo 1=>bar

+0

當我試圖找到解決方案時,我看到類似這樣的帖子。 ......不知道「結構」是什麼......但可能。會回到你身邊。我想我會給我'想法',因爲其他人對我的幫助很有幫助。 – brizz

+0

基於此:http://stackoverflow.com/questions/455910/what-is-the-function-construct-used-for.probably如此。想潛入,並且stackoverflow讓我:)。 ..將有點w /任何意見,以確認解決方案。 – brizz