繁体   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