1

配列の置換が正しく機能しないか、何か不足しています。

「読み取り中にエラーが発生しました」に等しいテキストを「XMLファイルで致命的なエラーが発生したため読み取りが停止しましたが、PHPコードを使用すると、キー0に対する値が更新されますが、これは間違っています!

何か案が?

ありがとう

元の状態:

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

正しいコードは次のとおりです。

$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 に答える
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 に答える