1

数组替换无法正常工作或我遗漏了一些东西。

我想将任何等于“读取时发生错误”的文本替换为“在 XML 文件中出现致命错误后读取已停止,但是当我使用低于键0的值的 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
)
4

3 回答 3

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>';
?>
于 2012-07-30T10:21:33.503 回答
0

脚本:

<?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 );
}

?>

输出$new_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] => Reading has stopped after fatal error in you XML file
)
于 2012-07-30T10:21:51.903 回答
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>'
于 2012-07-30T10:27:30.943 回答