2017-07-26 92 views
1

我有這些輸入數據,我插入到數據庫之前存儲在數組中。Foreach在php中的多維數組

$regData = array (
    'user_type'  => $_SESSION['user']->user_type, 
    'user_id'   => $_SESSION['user']->id, 
    'child_id'   => $this->input->post('child_name[]'), 
    'tc_id'   => $this->input->post('tc_id'), 
    'tc_sources'  => $this->input->post('tc_sources'), 
    'tc_created_date' => date('Y-m-d H:i:s'), 
); 

在陣列的輸出爲$regData

Array 
(
    [user_type] => parent 
    [user_id] => 5 
    [child_id] => Array 
     (
      [0] => 47 
      [1] => 49 
     ) 

    [tc_id] => 11 
    [tc_sources] => Email 
    [tc_created_date] => 2017-07-26 21:56:57 
) 

我想foreach陣列,以確保它可以被插入到不同的行中的表如下:

foreach ($regData as $k) { 
    foreach ($k as $val) { 
     $newArr[] = array(
      'user_type'  => $val['user_type'], 
      'user_id'   => $val['user_id'], 
      'child_id'   => $val['child_id'], 
      'tc_id'   => $val['tc_id'], 
      'tc_sources'  => $val['tc_sources'], 
      'tc_created_date' => $val['tc_created_date'], 
     ); 
    } 
} 

編輯

這是陣列形式我想要的輸出,但都具有相同值的數據,我真的不知道怎麼的foreach數組循環後得到確切的數據:

Array 
(
    [0] => Array 
     (
      [user_type] => 4 
      [user_id] => 4 
      [child_id] => 4 
      [tc_id] => 4 
      [tc_sources] => 4 
      [tc_created_date] => 4 
     ) 

    [1] => Array 
     (
      [user_type] => 4 
      [user_id] => 4 
      [child_id] => 4 
      [tc_id] => 4 
      [tc_sources] => 4 
      [tc_created_date] => 4 
     ) 

) 
+0

爲什麼不將concat數組vals設爲csv字符串? – ThisGuyHasTwoThumbs

+0

@ThisGuyHasTwoThumbs你喜歡用'implode'嗎? –

+0

yar - 如果你這樣做,你可以改變一個ID數組爲23,24,25然後爆炸,當你想要它作爲一個數組再次 – ThisGuyHasTwoThumbs

回答

0

請嘗試以下代碼,

if(!empty($regData['child_id'])) { 
    foreach($regData['child_id'] as $key => $row) { 
     $newArr[$key]['user_type'] = $regData['user_type']; 
     $newArr[$key]['user_id'] = $regData['user_id']; 
     $newArr[$key]['child_id'] = $row; 
     $newArr[$key]['tc_id'] = $regData['tc_id']; 
     $newArr[$key]['tc_sources'] = $regData['tc_sources']; 
     $newArr[$key]['tc_created_date'] = $regData['tc_created_date']; 
    } 
} 

輸出低於,

Array 
(
    [0] => Array 
    (
     [user_type] => parent 
     [user_id] => 5 
     [child_id] => 47 
     [tc_id] => 11 
     [tc_sources] => Email 
     [tc_created_date] => 2017-07-26 16:27:49 
    ) 

    [1] => Array 
    (
     [user_type] => parent 
     [user_id] => 5 
     [child_id] => 49 
     [tc_id] => 11 
     [tc_sources] => Email 
     [tc_created_date] => 2017-07-26 16:27:49 
    ) 

) 

你得到上面的輸出?如果不是,請通過評論來描述你的問題和樣本輸出。

+0

釘牢它!這就是我正在尋找的。謝謝! :) –

0

既然你叫你的變量$k,我想你認爲這將是你的數組的鍵,但它將是值而不是。

的第二個問題是,你的陣列$regData似乎只有一項,所以你甚至不需要你在所有

的foreach這應該工作:

$newArr[] = array(
     'user_type'  => $regData['user_type'], 
     'user_id'   => $regData['user_id'], 
     'child_id'   => $regData['child_id'], 
     'tc_id'   => $regData['tc_id'], 
     'tc_sources'  => $regData['tc_sources'], 
     'tc_created_date' => $regData['tc_created_date'], 
    ); 

如果你犯了一個錯誤在你的答案和$regData確實包含多個條目,那麼這應該工作:

foreach ($regData as $val) { 
    $newArr[] = array(
     'user_type'  => $val['user_type'], 
     'user_id'   => $val['user_id'], 
     'child_id'   => $val['child_id'], 
     'tc_id'   => $val['tc_id'], 
     'tc_sources'  => $val['tc_sources'], 
     'tc_created_date' => $val['tc_created_date'], 
    ); 
} 

使用foreach ($regData as $k => $val) {}到ALS Ø拿到鑰匙:

$newArr = array(); 
foreach ($regData as $entry) { 
    foreach ($entry as $key => $val) { 
     $newEntry[$key] = $val; 
    } 
    $newArr[] = $newEntry; 
} 

或許更好:

$newArr = array(); 
foreach ($regData as $entry) { 
    foreach ($entry as $key => $val) { 
     $newArr[$entry['user_id']][$key] = $val; 
    } 
} 

前提是你的user_id是唯一的值。

+0

我試過這個foreach但輸出不正確 –

+0

我'我發現了一個錯誤,之後我編輯了一些內容,並添加了一些可能會幫助你更多的案例,或者至少給你一些想法。 – ksjohn