簡體   English   中英

PHP中的Foreach多維數組

[英]Foreach multidimensional array in 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'],
        );
    }
}

編輯

這是我想要的數組形式的輸出,但是數據都具有相同的值,我真的不知道如何在循環后再讓數組獲取確切的數據:

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
        )

)

由於您調用了變量$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) {}也可以獲取密鑰:

$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是唯一值。

請嘗試以下代碼,

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
    )

)

上面有輸出嗎? 如果不是,請通過注釋描述您的問題和示例輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM