cakephp手动构建数组

时间:2013-10-13 01:15:18

标签: php arrays cakephp

我有一个报价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'
),

如何在阵列中插入多个申请人?

2 个答案:

答案 0 :(得分:0)

数组不能具有相同的id,但可以创建另一个数组:

$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);

输出:

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

如何使用ids?像这样:

foreach($ids as $key => $id) {
   // do whatever you want with the applicant id
}

答案 1 :(得分:0)

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

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

将输出:

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

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