我有一個名稱重複的函數。數組比較索引名稱失敗/如何比較數組的索引名稱
我想重複數組值,如果數組鍵== '重複'
但是我如果($滿足K == '重複')comparement失敗。
我的比較有什麼問題?
function repeat($schema, $repeat = array()){
foreach($schema as $k => $v){
如果($滿足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));
UPDATE1
架構可能是
$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',
)
)
);
更新,因爲你的$schema
變量沒有'repeat'
鍵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',
)
)
)
);
您的$ schema是嵌套數組。你應該修改它或迭代'$ schema [0]的'foreach'' – Voitcus
$ schema沒有固定值,總是可以改變。 $ schema [0]可能會失敗其他情況。我需要比較索引名稱的值 –
您可以檢查'is_array($ schema [0])',如果true將'$ schema'(您的本地變量)賦值給'$ schema [0]'。 – Voitcus