我有一个像这样的数组
Array
(
[sch_name] => example
[sch_degree] => example
[sch_field] => example
[sch_grade] => example
[sch_from_year] => 2008
[sch_to_year] => 2008
[sch_desc] => example
)
&安培;更像上面的数组,现在当我向它添加另一个数组时,它看起来如下所示
Array
(
[0] => Array
(
[sch_name] => example
[sch_degree] => example
[sch_field] => example
[sch_grade] => example
[sch_from_year] => 2008
[sch_to_year] => 2008
[sch_desc] => example
)
[1] => Array
(
[sch_name] => example
[sch_degree] => example
[sch_field] => example
[sch_grade] => example
[sch_from_year] => 2008
[sch_to_year] => 2008
[sch_desc] => example
)
)
现在问题是第三个数组被附加到[1]数组,如下所示
Array
(
[0] => Array
(
[sch_name] => example
[sch_degree] => example
[sch_field] => example
[sch_grade] => example
[sch_from_year] => 2008
[sch_to_year] => 2008
[sch_desc] => example
)
[1] => Array
(
[0] => Array
(
[sch_name] => example
[sch_degree] => example
[sch_field] => example
[sch_grade] => example
[sch_from_year] => 2008
[sch_to_year] => 2008
[sch_desc] => example
)
[1] => Array
(
[sch_name] => example
[sch_degree] => example
[sch_field] => example
[sch_grade] => example
[sch_from_year] => 2008
[sch_to_year] => 2008
[sch_desc] => example
)
)
)
我想将新数组附加为[2]&如果添加更多数组,那么它应该像这样[3]
这是我怎么做的
$post = array();
$post['sch_name'] = test_input($sch_name);
$post['sch_degree'] = test_input($sch_degree);
$post['sch_field'] = test_input($sch_field);
$post['sch_grade'] = test_input($sch_grade);
$post['sch_from_year'] = test_input($sch_from_year);
$post['sch_to_year'] = test_input($sch_to_year);
$post['sch_desc'] = test_input($sch_desc);
$newArray = array($post, $array_from_db);
$ array_from_db是从数据库中提取的,如我在问题开头所示
答案 0 :(得分:1)
如果要将值附加到数组,只需使用$array[] = $newArray;
,这会将新项目添加为最后一项;
$post = array();
$post['sch_name'] = test_input($sch_name);
$post['sch_degree'] = test_input($sch_degree);
$post['sch_field'] = test_input($sch_field);
$post['sch_grade'] = test_input($sch_grade);
$post['sch_from_year'] = test_input($sch_from_year);
$post['sch_to_year'] = test_input($sch_to_year);
$post['sch_desc'] = test_input($sch_desc);
$array_from_db[] = $post;
答案 1 :(得分:1)
您只需要一个容器数组来包含db中的所有数组。但
$newArray = array($post, $array_from_db);
这将创建一个新数组,而不是使用容器。
代码:
// declare a container
$container = array();
$post = array();
$post['sch_name'] = test_input($sch_name);
$post['sch_degree'] = test_input($sch_degree);
$post['sch_field'] = test_input($sch_field);
$post['sch_grade'] = test_input($sch_grade);
$post['sch_from_year'] = test_input($sch_from_year);
$post['sch_to_year'] = test_input($sch_to_year);
$post['sch_desc'] = test_input($sch_desc);
// push $post and $array_from_db to the $container
array_push($container,$post,$array_from_db);
顺便说一句,如果你只是想在数组中添加一个新元素,你可以简单地使用它:
$container[] = $myElement;
它保存调用函数(array_push)。
答案 2 :(得分:1)
根据我们的讨论,我们处于这个位置,你完全受益了。 discussion-between-frayne-konok-and-sagar-singh,假设您有来自数据库的数组,如下所示:
Online Check,必须检查。
$array_from_db = array(
array(
"sch_name" => "example",
"sch_degree" => "example",
"sch_field" => "example",
"sch_grade" => "example",
"sch_from_year" => "2008",
"sch_to_year" => "2008",
"sch_desc" => "example"
),
array(
"sch_name" => "example",
"sch_degree" => "example",
"sch_field" => "example",
"sch_grade" => "example",
"sch_from_year" => "2008",
"sch_to_year" => "2008",
"sch_desc" => "example"
)
);
新帖子:
$post = array();
$post['sch_name'] = "example";
$post['sch_degree'] = "example";
$post['sch_field'] = "example";
$post['sch_grade'] = "example";
$post['sch_from_year'] = "2008";
$post['sch_to_year'] = "2008";
$post['sch_desc'] = "example";
array_push($array_from_db, $post);
print_r($array_from_db);