2013-10-13 44 views
0

我有一個報價habtm申請人的關係。我正試圖獲得一份報價,以便一次保存多個申請人。我已經有了我需要的應用程序數組,但我不知道如何格式化該數組,以便在將它插入到引用數組中時保存它。 申請人陣列看起來像這樣:cakephp手動建立陣列

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 protected]', 
     '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' => '[email protected]', 
     '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' => '[email protected]', 
     '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' 
), 

如何插入多個申請人入陣?

回答

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 protected]', 
     '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' => '[email protected]', 
     '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' => '[email protected]', 
     '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 
} 
+0

我可以用set :: classicextract得到類似的東西,但是在我擁有它之後,我不是su關於如何處理它。 – user2787386

0

像tttony指出,數組索引必須是唯一的,所以我會:

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

將輸出:

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

現在的ID是關鍵$ IDS array ...你可以添加其他東西作爲值而不是null