插入一个数组

时间:2012-04-07 02:47:26

标签: php cakephp

我要将以下数组插入到包含字段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_idgrant_id)值(72,1,56); 插入表格(group_idapplication_idgrant_id)值(72,2,57);

我怎么做?

2 个答案:

答案 0 :(得分:1)

您可以使用Cake的Set :: 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 :(得分:-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
    }