2012-07-30 110 views
1

数组替换不能正常工作或者我错过了一些东西。array_search()错误

我想,以取代“读了致命错误后,停在你的XML文件,但是当我用下面针对关键值的PHP代码被更新“而阅读发生了错误”,这等于任何文本其是错的!

有什么想法?

感谢

原始状态:

Array 
(
    [0] => Element 'item', attribute 'isd': The attribute 'isd' is not allowed. 
    [1] => Element 'item', attribute 'avai0lable': The attribute 'avai0lable' is not allowed. 
    [2] => Unimplemented block at ..\xmlschemas.c:28274 
    [3] => An Error Occured while reading 
) 

PHP代码:

$errors = array_unique($errors); 
$key = array_search('An Error Occured while reading', $errors); 
$errors[$key] = 'Reading has stopped after fatal error in you XML file'; 
echo '<pre>'; print_r($errors); echo '</pre>'; 

故障结果:

Array 
(
    [0] => Reading has stopped after fatal error in you XML file 
    [1] => Element 'item', attribute 'avai0lable': The attribute 'avai0lable' is not allowed. 
    [2] => Unimplemented block at ..\xmlschemas.c:28274 
    [3] => Reading has stopped after fatal error in you XML file 
) 
+1

'var_dump($ key)'给你什么? – deceze 2012-07-30 10:14:45

+0

您确定这是您使用的确切代码吗?因为它工作正常或我。 – 2012-07-30 10:19:47

+0

bool(false)bool(false)bool(false)bool(false)bool(false)int(3) – BentCoder 2012-07-30 10:19:48

回答

0

正确的代码:

$errors = array_unique($errors); 
$key = array_search('An Error Occured while reading', $errors); 
if($key) 
$errors[$key] = 'Reading has stopped after fatal error in you XML file'; 
echo '<pre>'; print_r($errors); echo '</pre>' 
0

尝试下面的代码,我无法重现什么ü说:

<?php 
$errors = Array 
("Element 'item', attribute 'isd': The attribute 'isd' is not allowed.", 
    "Element 'item', attribute 'avai0lable': The attribute 'avai0lable' is not allowed.", 
    'Unimplemented block at ..\xmlschemas.c:28274', 
    'An Error Occured while reading', 
); 
echo '<pre>'; print_r($errors); echo '</pre>'; 
$errors = array_unique($errors); 
$key = array_search('An Error Occured while reading', $errors); 
$errors[$key] = 'Reading has stopped after fatal error in you XML file'; 
echo '<pre>'; print_r($errors); echo '</pre>'; 
?> 
0

脚本:

输出 $new_data
<?php 

$data = array (
    0 => "Element 'item', attribute 'isd': The attribute 'isd' is not allowed.", 
    1 => "Element 'item', attribute 'avai0lable': The attribute 'avai0lable' is not allowed.", 
    2 => "Unimplemented block at ..\xmlschemas.c:28274", 
    3 => "An Error Occured while reading" 
); 

$new_data = array(); 
$a = "An Error Occured while reading"; 
$b = "Reading has stopped after fatal error in you XML file"; 

foreach ($data as $key => $value) { 
    $new_data[$key] = str_replace($a, $b, $value); 
} 

?> 

Array 
(
    [0] => Element 'item', attribute 'isd': The attribute 'isd' is not allowed. 
    [1] => Element 'item', attribute 'avai0lable': The attribute 'avai0lable' is not allowed. 
    [2] => Unimplemented block at ..\xmlschemas.c:28274 
    [3] => Reading has stopped after fatal error in you XML file 
)