迭代php多维数组并为每个数组插入记录

时间:2014-08-01 18:13:32

标签: php mysql arrays

$array = array (
    'data' => 
    array (
      'employment' => 
      array (
        'df1e' => 
        array (
          0 => '',
        ),
        'dt1e' => 
        array (
          0 => '',
        ),
        'c1e' => 
        array (
          0 => '',
        ),
        'p1e' => 
        array (
          0 => '',
        ),
        'l1e' => 
        array (
          0 => '',
        ),
      ),
      'address' => 
      array (
        'df1a' => 
        array (
          0 => '',
        ),
        'dt1a' => 
        array (
          0 => '',
        ),
        'sa1a' => 
        array (
          0 => '',
        ),
        'c1a' => 
        array (
          0 => '',
        ),
        'st1a' => 
        array (
          0 => 'AL',
        ),
        'zc1a' => 
        array (
          0 => '',
        ),
      ),
    ),
    'id' => '709ce406-8fd7-f8ef-6438-53dbbeee4151',
    'name' => 'test'
);

上面的数组是根据用户输入生成的。这是地址和工作经历的记录。

df1e =日期来自。

如果用户输入多个就业历史记录,则可能有多个df1e字段。如果用户填写了3个表格,则每个数组中的ech值(df1e,dt1e等)将有3个值。

如何迭代此数组并为每个数组插入一条记录?还有地址历史。

每条记录应该是分开的。

这是我的尝试不太有效。它插入一条记录,但值不对。

if($data) {
        foreach($data as $history) {
                foreach($history['employment'] as $employment) {
                        $bean->name = $data['name'] . ' - Employment';
                        $bean->type_c = 'Employment';
                        $bean->date_from_c = $employment['df1e'];
                        $bean->date_to_c = $employment['dt1e'];
                        $bean->company_c = $employment['c1e'];
                        $bean->position_c = $employment['p1e'];
                        $bean->location_c = $employment['l1e'];
                }
                foreach($history['address'] as $address) {
                        $bean->name = $data['name'] . ' - Address';
                        $bean->type_c = 'Address';
                        $bean->date_from_c = $address['df1a'];
                        $bean->date_to_c = $address['dt1a'];
                        $bean->street_address_city_c = $address['c1a'];
                        $bean->street_address_state_c = $address['st1a'];
                        $bean->street_address_postalcode_c = $address['zc1a'];
                        $bean->street_address_c = $address['sa1a'];
                }
        }

        $bean->save();
}

0 个答案:

没有答案