2012-04-07 209 views
0

我有以下陣列,我要插入到與領域表groupi_id,APPLICATION_ID和grant_id插入陣列

陣列是

array(
    'ApplicationsGrant' => array(
     'group_id' => array(
      (int) 0 => '72', 
      (int) 1 => '72' 
     ), 
     'application_id' => array(
      (int) 0 => '1', 
      (int) 1 => '2' 
     ), 
     'grant_id' => array(
      (int) 0 => 56, 
      (int) 1 => 57 
     ) 
    ) 
) 

欲每個子即插入行數組使用數組鍵。所以在上面的例子中會插入2行像這樣

插入表格(group_idapplication_id,grant_id)值(72,1,56); 插入表格(group_idapplication_idgrant_id)值(72,2,57);

我該怎麼做?

回答

1

您可以使用Cake的集:: classicExtract()拉出值。我假設你知道如何將它們保存到數據庫中。

http://book.cakephp.org/2.0/en/core-utility-libraries/set.html

在你的情況,像(未經測試):

$result1 = Set::classicExtract($a, '{n}.{s}.{s}.0'); 
$result2 = Set::classicExtract($a, '{n}.{s}.{s}.1'); 

如果您需要的按鍵,就可以提取那些第一:

$fields = Set::classicExtract($a, '{n}.{s}.{s}'); 
-1

我已經試過這一個

$d = array(
     'ApplicationsGrant' => array(
      'group_id' => array(
       (int) 0 => '72', 
       (int) 1 => '72' 
      ), 
      'application_id' => array(
       (int) 0 => '1', 
       (int) 1 => '2' 
      ), 
      'grant_id' => array(
       (int) 0 => 56, 
       (int) 1 => 57 
      ) 
     ) 
    ); 
    for ($i=0; $i<count($d['ApplicationsGrant']['group_id']); $i++) { 
     echo $d['ApplicationsGrant']['group_id'][$i]."<br/>"; //outputs 72, 72 
    }