I have confused here, i try many time to close this html array tags with an php function but i can't! Can help any professional developer?
The array below needs to make an html output like this below, but how can do that?
The parents key ( [parents] => 1 ) tell to as that tag have a parents, and the key ( [level] => 2 ) tell to as the level belongs. How can close it correct?
The OUTPUT:
<form class="HORIZONTAL">
<div class="firstclass">FIRSTCLASS</div>
<div class="form-group">
<label class="col-sm-3 control-label">TEXT
<div class="col-sm-9">
<span class="myspan">MYSPAN
<div class="myclass">MYCLASS
</div>
</span>
</div>
</label>
</div>
<div class="firstclass">FIRSTCLASS</div>
<div class="form-group">
<label class="col-sm-3 control-label">TEXT
<div class="col-sm-9">
<span class="myspan">MYSPAN
<div class="myclass">MYCLASS
</div>
</span>
</div>
</label>
</div>
<div class="firstclass">FIRSTCLASS</div>
</form>
The PHP array:
$data = [
[
'tag' => 'form',
'options' => ' class="HORIZONTAL"',
'text' => false,
'level' => 1,
'parents' => true,
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
'level' => 2,
'parents' => false,
],
[
'tag' => 'div',
'options' => ' class="form-group"',
'text' => 'FORMGROUP',
'level' => 2,
'parents' => true,
],
[
'tag' => 'label',
'options' => ' class="col-sm-3 control-label"',
'text' => 'TEXT',
'level' => 3,
'parents' => true,
],
[
'tag' => 'div',
'options' => ' class="col-sm-9"',
'text' => false,
'level' => 4,
'parents' => true
],
[
'tag' => 'span',
'options' => ' class="myspan"',
'text' => 'MYSPAN',
'level' => 5,
'parents' => true
],
[
'tag' => 'div',
'options' => ' class="myclass"',
'text' => 'MYCLASS',
'level' => 6,
'parents' => false
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
'level' => 2,
'parents' => false
],
[
'tag' => 'div',
'options' => ' class="form-group"',
'text' => 'FORMGROUP',
'level' => 2,
'parents' => true
],
[
'tag' => 'label',
'options' => ' class="col-sm-3 control-label"',
'text' => 'TEXT',
'level' => 3,
'parents' => true
],
[
'tag' => 'div',
'options' => ' class="col-sm-9"',
'text' => false,
'level' => 4,
'parents' => true
],
[
'tag' => 'span',
'options' => ' class="myspan"',
'text' => 'MYSPAN',
'level' => 5,
'parents' => true
],
[
'tag' => 'div',
'options' => ' class="myclass"',
'text' => 'MYCLASS',
'level' => 6,
'parents' => false
],
[
'tag' => 'div',
'options' => ' class="firstclass"',
'text' => 'FIRSTCLASS',
'level' => 2,
'parents' => false
]
];
That is my function but is wrong i think :)
function get($data) {
$top= '';
$botttom= '';
foreach($data as $key => $row) {
$tag = isset($row['tag']) ? $row['tag'] : false;
$text = isset($row['text']) ? $row['text'] : false;
$level = isset($row['level']) ? $row['level'] : false;
$options = isset($row['options']) ? $row['options'] : false;
$parents = isset($row['parents']) ? $row['parents'] : false;
$parent_level = isset($row['parent_level']) ? $row['parent_level'] : false;
$openTag = '<'.$tag.$options.'>'.$text;
$closeTag = '</'.$tag.'>';
if($parents) {
$top.= $openTag;
} else {
$botttom.= $closeTag;
}
}
$result = $top.$botttom;
return $result;
}
print_r(get($data));
Aucun commentaire:
Enregistrer un commentaire