【问题标题】:Foreach multidimensional array in phpphp中的foreach多维数组
【发布时间】:2017-07-26 14:05:44
【问题描述】:

在插入数据库之前,我将这些输入数据存储在数组中。

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

)

【问题讨论】:

  • 为什么不将数组 vals 连接为 csv 字符串?
  • @ThisGuyHasTwoThumbs 你的意思是喜欢使用implode吗?
  • yar - 如果你这样做,你可以将一个 id 数组更改为 23,24,25,然后在你希望它再次作为数组时在 , 上爆炸

标签: php arrays multidimensional-array foreach


【解决方案1】:

请尝试以下代码,

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
    )

)

你得到上面的输出了吗?如果不是,请通过评论描述您的问题和示例输出。

【讨论】:

  • 成功了!这就是我正在寻找的。谢谢! :)
【解决方案2】:

既然你调用了你的变量$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 是唯一值。

【讨论】:

  • 我试过这个foreach但是输出不正确
  • 我发现了一个错误,我立即编辑并添加了更多案例,这些案例可能对您有更多帮助或至少给您一些想法。
猜你喜欢
  • 2012-05-01
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 2018-03-01
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多