2012-07-30 63 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 
)