将PHP数组转换为HTML表单输入是复制项目 [英] Converting a PHP array into HTML form inputs is duplicating items
问题描述
我正在尝试使用数组
值并从这些值中构建一个php表单。
数组处于最底层以保持问题清晰。数组结构是:
- 项目
- 项目
- 项目包含子
- 项目
- 项目
- 包含子项目的项目
- 项目
- 项目
这是我想要输出每个项目
但是的项目,如果项目有一个子项目,只需输出父项目的名称并为孩子创建字段。
I创建了这个:
函数renderTest2(array $ data)
{
$ html ='< ul> ;';
foreach($ data as $ item){
$ html。='< li>';
foreach($ item as $ key => $ value){
if(is_array($ value)){
$ html。= renderTest2($值);
} else {
if(array_key_exists('children',$ item)){
$ html。= $ item ['name'];
} else {
$ html。= $ item ['name']。 < input type = \text \value = \\>< br />;
}
}
}
$ html。='< / li>';
}
$ html。='< / ul>';
返回$ html;
}
这给了我这个输出:
但我不明白它为什么重复项目。这是我使用的测试数组:
<$ c $ c> $ aFullArray = array();
$ aFullArray [] = array(name=>Adam,address=>123 main,phone=>000-000-0000) ;
$ aFullArray [] = array(name=>Beth,address=>123 main,phone=>000-000-0000);
$ aChildren = array();
$ aChildren [] = array(name=>Mike,address=>123 main,phone=>000-000-0000);
$ aChildren [] = array(name=>Nancy,address=>123 main,phone=>000-000-0000);
$ subChild = array();
$ subChild [] = array(name=>Peter,address=>123 main,phone=>000-000-0000);
$ subChild [] = array(name=>Paul,address=>123 main,phone=>000-000-0000);
$ aChildren [] = array(name=>Oscar,address=>123 main,phone=>000-000-0000,
children=> $ subChild);
$ b $ aFullArray [] = array(name=>Charlie,address=>123 main,phone=>000-000-0000,
children=> $ aChildren);
$ aFullArray [] = array(name=>Danny,address=>123 main,phone=>000-000-0000) ;
function renderTest2 (array $ data)
{
$ html ='< ul>';
foreach($ data as $ item){
$ html。='< li>';
if(array_key_exists('children',$ item)){
$ html。= $ item ['name'];
$ html。= renderTest2($ item ['children']);
} else {
$ html。= $ item ['name']。 < input type ='text'value =''>< br />;
}
$ html。='< / li>';
}
$ html。='< / ul>';
返回$ html;
}
你循环两次,这是不需要的,如果它有孩子传递孩子仅限数组
解释:
- 循环一个数组,如果它有
children
key dorecursion
- 如果找不到生成li元素
I am attempting to take an array
of values and build a php form from those values.
The array is at the bottom to keep the question clear. The array structure is:
- Item
- Item
- Item with Child
-Item
-Item
- Item with Child
-Item
-Item
Here is what I want to output each item
but if the item has a child, just output the name of the parent and create fields for the children.
I created this:
function renderTest2(array $data)
{
$html = '<ul>';
foreach ($data as $item) {
$html .= '<li>';
foreach ($item as $key => $value) {
if (is_array($value)) {
$html .= renderTest2($value);
} else {
if (array_key_exists('children', $item)) {
$html .= $item['name'];
} else {
$html .= $item['name'] . "<input type=\"text\" value=\"\"> <br/>";
}
}
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
Which gave me this output:
But I don't understand why it is duplicating items. What am I doing wrong?
Here is the test array I used:
$aFullArray = array();
$aFullArray[] = array("name" => "Adam", "address" => "123 main", "phone" => "000-000-0000");
$aFullArray[] = array("name" => "Beth", "address" => "123 main", "phone" => "000-000-0000");
$aChildren = array();
$aChildren [] = array("name" => "Mike", "address" => "123 main", "phone" => "000-000-0000");
$aChildren[] = array("name" => "Nancy", "address" => "123 main", "phone" => "000-000-0000");
$subChild = array();
$subChild [] = array("name" => "Peter", "address" => "123 main", "phone" => "000-000-0000");
$subChild [] = array("name" => "Paul", "address" => "123 main", "phone" => "000-000-0000");
$aChildren [] = array("name" => "Oscar", "address" => "123 main", "phone" => "000-000-0000",
"children" => $subChild);
$aFullArray[] = array("name" => "Charlie", "address" => "123 main", "phone" => "000-000-0000",
"children" => $aChildren);
$aFullArray[] = array("name" => "Danny", "address" => "123 main", "phone" => "000-000-0000");
function renderTest2(array $data)
{
$html = '<ul>';
foreach ($data as $item) {
$html .= '<li>';
if (array_key_exists('children', $item)) {
$html .= $item['name'];
$html .= renderTest2($item['children']);
} else {
$html .= $item['name'] . "<input type='text' value=''> <br/>";
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
you are looping two times which is not required and if it has children pass children array only
Explanation:
- Loop through an array and if it has
children
key dorecursion
- If not found generate li element
这篇关于将PHP数组转换为HTML表单输入是复制项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!