Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中重新格式化给定数组_Php_Arrays - Fatal编程技术网

如何在php中重新格式化给定数组

如何在php中重新格式化给定数组,php,arrays,Php,Arrays,我从一个html表单帖子中获取多个数据库表的变量数据。Post数组需要格式化以用作数组,以更新来自表中的数据 文章中的数组: Array ( [test_id] => 1 [appl_id] => 1 [Test_code] => 0 [test_date] => 2017-03-09 [test_type] => 46 ) 我需要的最佳结果是通过foreach循环运行它,并根据需要格式化键和值: $data = ""; f

我从一个html表单帖子中获取多个数据库表的变量数据。Post数组需要格式化以用作数组,以更新来自表中的数据

文章中的数组:

Array
(
    [test_id] => 1
    [appl_id] => 1
    [Test_code] => 0
    [test_date] => 2017-03-09
    [test_type] => 46
)
我需要的最佳结果是通过foreach循环运行它,并根据需要格式化键和值:

$data = "";
foreach($_POST as $key => $value){
    $data .= "'".$key."' => '".$value."',<br />";
}

TblAdmin::update($table, array($data), $param, $id);
我需要将数组重新格式化为:

Array
(
    'test_id' => '1',
    'appl_id' => '1',
    'Test_code' => '0',
    'test_date' => '2017-03-09',
    'test_type' => '46',
)

你知道,好的,Post数组按原样工作。问题在于数据库中的某些字段仍然为空,并且尝试使用具有无值键的PDO语句更新MariaDB中的字段是无效的。一旦我将空值设置为null或0,一切正常。
总之,我在错误的位置查找错误。

在代码中,您正在使用中的内容构建一个字符串,而它恰好将其显示为一个数组。但是为什么您只想将$u POST复制到$data?我必须更新表数据的方法需要这种格式,这种格式在所有情况下都适用。为了便于表的维护,我编写了一个例程,它将数据从一个x表拉入一个动态表单,在这个表单中数据可以更新并发送回数据库。为了将数据存储回数据库,我需要拆开post数组并将其放入一个查询中以更新数据库。同意Nigel Ren的观点,这似乎有点粗略。无论如何,根据您提到的,您的代码永远不应该生成[0]这个东西:您是对的,变量$data不会生成[0]。这是在这里将字符串转换回数组时产生的:TblAdmin::update$table,array$data,$param,$id;这是因为您不应该将字符串转换为数组。如果要添加所有数据,为什么不发送TblAdmin::更新$\u POST?
Array
(
    'test_id' => '1',
    'appl_id' => '1',
    'Test_code' => '0',
    'test_date' => '2017-03-09',
    'test_type' => '46',
)