0

我有一个引用 habtm 申请人的关系。我正在尝试获取报价以同时保存多个申请人。我已经有一个我需要的申请人数组,但我不知道如何格式化该数组以在我将其插入报价数组时保存它。申请人数组如下所示:

 array(
(int) 0 => array(
    'Applicant' => array(
        'id' => '436',
        'clientcase_id' => '66',
        'archive_id' => '1',
        'birthdate' => '2013-09-21 01:41:00',
        'title' => '',
        'first_name' => 'george',
        'middle_name' => 'a',
        'surname' => 'summerlane',
        'email' => 'email@q.com',
        'landline_number' => '88465120.',
        'mobile_number' => '',
        'applicant_type' => '',
        'created' => '2013-09-21 01:43:10',
        'modified' => '2013-09-21 01:43:10'
    )
),
(int) 1 => array(
    'Applicant' => array(
        'id' => '435',
        'clientcase_id' => '66',
        'archive_id' => '1',
        'birthdate' => '2013-09-21 01:41:00',
        'title' => '',
        'first_name' => 'mary',
        'middle_name' => 's',
        'surname' => 'amnn',
        'email' => 'some@this.cin',
        'landline_number' => '465132',
        'mobile_number' => '',
        'applicant_type' => '',
        'created' => '2013-09-21 01:41:48',
        'modified' => '2013-09-21 01:41:48'
    )
),
(int) 2 => array(
    'Applicant' => array(
        'id' => '66',
        'clientcase_id' => '66',
        'archive_id' => '1',
        'birthdate' => null,
        'title' => null,
        'first_name' => 'Tania',
        'middle_name' => '',
        'surname' => 'Humphreys',
        'email' => 'purple67@me.com',
        'landline_number' => null,
        'mobile_number' => '0438854355',
        'applicant_type' => 'Main applicant',
        'created' => '2012-10-29 00:00:00',
        'modified' => '2012-10-21 00:00:00'
    )
)

)

我需要一个看起来像这样的:

array(
'Applicants' => array(
    'id' => 435,
    'id' => 436,
    'id' => 66
)

)

我该怎么做呢?或者,还有更好的方法?

当我保存报价时,数组如下所示:

array(
'QuoteButton' => 'Submit',
'Quote' => array(
    'date' => array(
        'day' => '13',
        'month' => '10',
        'year' => '2013'
    ),
    'description' => '',
    'quote_accepted' => '0',
    'research_accepted' => '0',
    'cc_accepted' => '0',
    'pesel_accepted' => '0',
    'setfees_accepted' => '0',
    'total' => '0'
),
'Applicant' => array(
    'id' => '66'
),

如何在数组中插入多个申请人?

4

2 回答 2

0

An array can't have the same id, but can crate another array like this:

$datas = array(
(int) 0 => array(
    'Applicant' => array(
        'id' => '436',
        'clientcase_id' => '66',
        'archive_id' => '1',
        'birthdate' => '2013-09-21 01:41:00',
        'title' => '',
        'first_name' => 'george',
        'middle_name' => 'a',
        'surname' => 'summerlane',
        'email' => 'email@q.com',
        'landline_number' => '88465120.',
        'mobile_number' => '',
        'applicant_type' => '',
        'created' => '2013-09-21 01:43:10',
        'modified' => '2013-09-21 01:43:10'
    )
),
(int) 1 => array(
    'Applicant' => array(
        'id' => '435',
        'clientcase_id' => '66',
        'archive_id' => '1',
        'birthdate' => '2013-09-21 01:41:00',
        'title' => '',
        'first_name' => 'mary',
        'middle_name' => 's',
        'surname' => 'amnn',
        'email' => 'some@this.cin',
        'landline_number' => '465132',
        'mobile_number' => '',
        'applicant_type' => '',
        'created' => '2013-09-21 01:41:48',
        'modified' => '2013-09-21 01:41:48'
    )
),
(int) 2 => array(
    'Applicant' => array(
        'id' => '66',
        'clientcase_id' => '66',
        'archive_id' => '1',
        'birthdate' => null,
        'title' => null,
        'first_name' => 'Tania',
        'middle_name' => '',
        'surname' => 'Humphreys',
        'email' => 'purple67@me.com',
        'landline_number' => null,
        'mobile_number' => '0438854355',
        'applicant_type' => 'Main applicant',
        'created' => '2012-10-29 00:00:00',
        'modified' => '2012-10-21 00:00:00'
    )
)

);

$ids = array();
foreach($datas as $data => $applicants) {
    $ids[] = $applicants['Applicant']['id'];
}

print_r($ids);

Output:

Array ( [0] => 436 [1] => 435 [2] => 66 ) 

How to use the ids? Like this:

foreach($ids as $key => $id) {
   // do whatever you want with the applicant id
}
于 2013-10-13T01:48:00.257 回答
0

就像 tttony 指出的那样,数组索引需要是唯一的,所以我会:

$ids = array();
foreach($array as $applicant)
  $ids[$applicant['Applicant']['id']] = null;

这将输出:

Array
(
  [436] => 
  [435] => 
  [66] => 
)

现在 id 是 $ids 数组的关键...您可以添加其他内容作为值而不是 null

于 2013-10-13T02:41:00.103 回答