我正在尝试在具有无限数量的父级和子级的选择框内创建树。 我的选择框(html部分)应该是Access,我在下面给出了一个图表。
我有一个"类别"数组有两个主数组,节点数不限(父子)。两个主要数组名称为[scope] => selectboxFirst
和[scope] => selectboxsecond
。有parentid
以4000
开头。我无法在代码中硬编码parentid
。数组来自解码的json字符串。
我不知道如何遍历此数组并在单独的选择框中显示这两个数组。
在我发布的示例数组中,一个数组中有两个主数组,但它们也可以存在于其他数组中。我不知道如何在两个选择框中显示这个作为父子。
所有[scope] => selectboxFirst
类别都会显示在第一个选择框中,[scope] => selectboxsecond
类别会显示在第二个选择框中。
Array
(
[status] => Array
(
[message] => Good
[code] => 200
)
[categories] => Array
(
[0] => Array
(
[scope] => selectboxFirst
[categories] => Array
(
[0] => Array
(
[id] => 4001
[parentId] => 4000
[name] => Access
[children] => Array
(
[0] => Array
(
[id] => 4010
[parentId] => 4001
[name] => mine
[type] => Reference
)
[1] => Array
(
[id] => 4011
[parentId] => 4001
[name] => yours
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4002
[parentId] => 4000
[name] => Communication
[children] => Array
(
[0] => Array
(
[id] => 4015
[parentId] => 4002
[name] => Physician
[children] => Array
(
[0] => Array
(
[id] => 4016
[parentId] => 4015
[name] => Helps blsys Understand
[type] => Reference
)
[1] => Array
(
[id] => 4017
[parentId] => 4015
[name] => Listens
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4021
[parentId] => 4002
[name] => Clinical ps
[children] => Array
(
[0] => Array
(
[id] => 4022
[parentId] => 4021
[name] => yours ps/datasssss
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4024
[parentId] => 4002
[name] => etc
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4003
[parentId] => 4000
[name] => Office
[children] => Array
(
[0] => Array
(
[id] => 4026
[parentId] => 4003
[name] => Facilities/Environment
[children] => Array
(
[0] => Array
(
[id] => 4027
[parentId] => 4026
[name] => Noise
[type] => Reference
)
[1] => Array
(
[id] => 4028
[parentId] => 4026
[name] => Lighting
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4032
[parentId] => 4003
[name] => Office ps
[children] => Array
(
[0] => Array
(
[id] => 4033
[parentId] => 4032
[name] => mine ps
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4035
[parentId] => 4003
[name] => Billing
[type] => Reference
)
)
[type] => Reference
)
[3] => Array
(
[id] => 4004
[parentId] => 4000
[name] => Outgo
[children] => Array
(
[0] => Array
(
[id] => 4037
[parentId] => 4004
[name] => Diagnosis
[type] => Reference
)
[1] => Array
(
[id] => 4038
[parentId] => 4004
[name] => Quality of Procedure
[type] => Reference
)
)
[type] => Reference
)
[4] => Array
(
[id] => 4005
[parentId] => 4000
[name] => Loyalty
[children] => Array
(
[0] => Array
(
[id] => 4039
[parentId] => 4005
[name] => Likely to also
[type] => Reference
)
)
[type] => Reference
)
)
)
[1] => Array
(
[scope] => selectboxSecond
[categories] => Array
(
[0] => Array
(
[id] => 4251
[parentId] => 4250
[name] => Communication with datas
[children] => Array
(
[0] => Array
(
[id] => 4262
[parentId] => 4251
[name] => respect?
[type] => Reference
)
[1] => Array
(
[id] => 4263
[parentId] => 4251
[name] => you?
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4252
[parentId] => 4250
[name] => Communication with Doctors
[children] => Array
(
[0] => Array
(
[id] => 4266
[parentId] => 4252
[name] => you with courtesy
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4253
[parentId] => 4250
[name] => Responsiveness of data ps
[children] => Array
(
[0] => Array
(
[id] => 4269
[parentId] => 4253
[name] => During
[type] => Reference
)
[1] => Array
(
[id] => 4270
[parentId] => 4253
[name] => How
[type] => Reference
)
)
[type] => Reference
)
[3] => Array
(
[id] => 4254
[parentId] => 4250
[name] => Pain Management
[children] => Array
(
[0] => Array
(
[id] => 4271
[parentId] => 4254
[name] => During
[type] => Reference
)
)
[type] => Reference
)
[4] => Array
(
[id] => 4255
[parentId] => 4250
[name] => Communication about Medicines
[children] => Array
(
[0] => Array
(
[id] => 4274
[parentId] => 4255
[name] => During this data stay
[type] => Reference
)
[1] => Array
(
[id] => 4275
[parentId] => 4255
[name] => Before giving you
[type] => Reference
)
[2] => Array
(
[id] => 4276
[parentId] => 4255
[name] => Before giving you
[type] => Reference
)
)
[type] => Reference
)
[5] => Array
(
[id] => 4256
[parentId] => 4250
[name] => Discharge Information
[children] => Array
(
[0] => Array
(
[id] => 4277
[parentId] => 4256
[name] => During this data stay the data?
[type] => Reference
)
[1] => Array
(
[id] => 4278
[parentId] => 4256
[name] => After you left the data
[type] => Reference
)
[2] => Array
(
[id] => 4279
[parentId] => 4256
[name] => During this data stay
[type] => Reference
)
)
[type] => Reference
)
[6] => Array
(
[id] => 4257
[parentId] => 4250
[name] => Care Transition
[children] => Array
(
[0] => Array
(
[id] => 4280
[parentId] => 4257
[name] => During this data stay
[type] => Reference
)
)
[type] => Reference
)
[7] => Array
(
[id] => 4258
[parentId] => 4250
[name] => Cleanliness of data Environment
[children] => Array
(
[0] => Array
(
[id] => 4282
[parentId] => 4258
[name] => During this data?
[type] => Reference
)
)
[type] => Reference
)
[8] => Array
(
[id] => 4259
[parentId] => 4250
[name] => Quietness of data Environment
[children] => Array
(
[0] => Array
(
[id] => 4283
[parentId] => 4259
[name] => During this data?
[type] => Reference
)
)
[type] => Reference
)
[9] => Array
(
[id] => 4260
[parentId] => 4250
[name] => also the data
[children] => Array
(
[0] => Array
(
[id] => 4284
[parentId] => 4260
[name] => Would you?
[type] => Reference
)
)
[type] => Reference
)
[10] => Array
(
[id] => 4261
[parentId] => 4250
[name] => Overall Rating of data
[children] => Array
(
[0] => Array
(
[id] => 4285
[parentId] => 4261
[name] => Using any number
[type] => Reference
)
)
[type] => Reference
)
)
)
)
)
输出结果类似于:例如:ID应该与selectbox一起进入selectbox 访问
Electronics
--Keyboards
--Phones
----HTC DESIRE Z
----HTC ONE X
-------Camera
-------Battety
Food
--Fruits
----Apple
----Watermelon
--Vegetables
----Carrot
----Onion
**inside select box . for eg: <select><option value='4001'>Access</option></select>**
到目前为止,我已经做了很多,但它显示了一切。仍然没有得到
$json = json_decode($string,TRUE);
//echo "<pre>";
//print_r($json);
recursive($json,$level=1);
function recursive($array, $level = 1){
foreach($array as $value){
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
recursive($value, $level + 1);
} else{
//It is not an array, so print it out.
echo str_repeat("-", $level), $value, '<br>';
}
}
}
答案 0 :(得分:2)
以下代码将通过您的数据结构进行递归并创建选项列表。我将为您创建select
标签,因为它非常简单。我已经使用PHP_EOL
在每个选项后添加换行符,以便更容易阅读HTML代码。
foreach ($data['categories'] as $c) {
# scope is $c['scope']
echo "Starting " . $c['scope'] . PHP_EOL;
# calling the function on the categories data
recurse($c['categories']);
echo "Finished " . $c['scope'] . PHP_EOL;
}
function recurse($arr, $level = 0){
# we have a numerically-indexed array. go through each item:
foreach ($arr as $n) {
# print out the item ID and the item name
echo '<option value="' . $n['id'] . '">'
. str_repeat("-", $level)
. $n['name']
. '</option>'
. PHP_EOL;
# if item['children'] is set, we have a nested data structure, so
# call recurse on it.
if (isset($n['children'])) {
# we have children: RECURSE!!
recurse( $n['children'], $level+1);
}
}
}
输出:
Starting selectboxFirst
<option value="4001">Access</option>
<option value="4010">-mine</option>
<option value="4011">-yours</option>
<option value="4002">Communication</option>
<option value="4015">-Physician</option>
<option value="4016">--Helps blsys Understand</option>
<option value="4017">--Listens</option>
<option value="4021">-Clinical ps</option>
<option value="4022">--yours ps/datasssss</option>
<option value="4024">-etc</option>
<option value="4003">Office</option>
<option value="4026">-Facilities/Environment</option>
<option value="4027">--Noise</option>
(等)
您可以将相应的代码替换为&#34;启动selectboxFirst / selectboxSecond&#34;。