我有一个名称是重复的函数。
如果数组键 == 'repeat' 我想重复数组值
但是我的 if ($k == 'repeat') 比较失败了。
我的比较有什么问题?
function repeat($schema, $repeat = array()){
foreach($schema as $k => $v){
if($k == '重复')
第 3 行在重复功能内无法正常工作。
$schema = array(
array(
'tag' => 'div',
'class' => 'lines',
'repeat' => array(
'tag' => 'div',
array(
'tag' => 'span',
'style' => 'margin:10px; padding:10px',
'key' => 'title',
),
'key' => 'subject',
)
)
);
$repeat = array('Country Name' => 'Usa', 'City Name' => 'Newyork');
// Recursive String Replace - recursive_array_replace(mixed, mixed, array);
function recursive_array_replace($find, $replace, $array){
if (!is_array($array)){
return str_replace($find, $replace, $array);
}
$newArray = array();
foreach ($array as $key => $value) {
$newArray[$key] = recursive_array_replace($find, $replace, $value);
}
return $newArray;
}
function repeat($schema, $repeat = array()){
foreach($schema as $k => $v){
if($k == 'repeat'){
foreach($repeat as $rk => $rv){
$value = recursive_array_replace('title', $rk, $v);
$value = recursive_array_replace('subject', $rv, $value);
$info[] = $value;
}
}
}
//$schema = recursive_array_replace('repeat', $repeat, $schema);
return $info;
}
print_r(repeat($schema, $repeat));
更新1
架构可能是
$schema = array(
'tag' => 'div',
'class' => 'lines',
array(
'tag' => 'div',
'class' => 'lines',
'repeat' => array(
'tag' => 'div',
array(
'tag' => 'span',
'style' => 'margin:10px; padding:10px',
'key' => 'title',
),
'key' => 'subject',
array(
'tag' => 'span',
'style' => 'margin:10px; padding:10px',
'key' => 'title',
),
'key' => 'subject',
)
)
);
更新 2
$schema = array(
array(
'tag' => 'div',
'class' => 'lines',
array(
'tag' => 'div',
'repeat' => array(
'tag' => 'div',
array(
'tag' => 'span',
'style' => 'margin:10px; padding:10px',
'key' => 'title',
),
'key' => 'subject',
)
)
)
);