Is it possible to send array key names inside function
I have small function which creates html output according to my $schema
array structure.
Is it possible to have same output with my new array structure? (Is it
possible to send key name of arrays inside function)
My original array structure.
$schema = array(
array(
'tag' => 'div',
'class' => 'lines',
array(
'tag' => 'div',
array(
'tag' => 'span',
'style' => 'margin:10px; padding:10px',
'key' => '$key-countryname',
),
'key' => '$value-country',
),
array(
'tag' => 'div',
array(
'tag' => 'span',
'style' => 'margin:10px; padding:10px',
'key' => '$key-countryname',
),
'key' => '$value-country',
),
)
);
New array structure that I want to have same output with function
$schema = array(
'div' => array(
'class' => 'lines',
'div' => array(
'span' => array(
'style' => 'margin:10px; border:10px',
'key' => '$key-countryname',
),
'key' => '$value-country',
)
'div' => array(
'span' => array(
'style' => 'margin:10px; border:10px',
'key' => '$key-countryname',
),
'key' => '$value-country',
)
)
);
My function
$vals = array('boat name' => 'miracle', 'display' => 'denix');
function get_output($schema, $vals, $t = -1){
$t++; $tag = ""; $atts = array(); $keys = array(); $code = array();
foreach($schema as $k => $v){
if(is_array($v)){
$keys[] = get_output($v, $vals, $t);
} else {
switch($k){
case "tag": $tag = $v; break;
case "key": $keys[] = $v; break;
case "type": break;
default: $atts[$k] = $v; break;
}
}
}
$code[] = "\n".str_repeat("\t", $t);
if($tag){
$code[] = "<$tag"; foreach($atts as $k=>$v){ $code[] = '
'.$k.'="'.$v.'"'; } $code[] = ">";
$code = array(implode('', $code));
}
foreach($keys as $k){ $code[] = $k; }
if($tag){
$code[] = "\n".str_repeat("\t", $t);
$code[] = '</'.$tag.'>';
}
//print_r($code);
return implode("", $code);
}
echo get_output($schema, $vals, -1);
Thanks.
No comments:
Post a Comment