PHP-插入XML文件并从中获取

PHP-插入XML文件并从中获取,php,arrays,xml,json,get,Php,Arrays,Xml,Json,Get,我使用JSON为聊天系统编写了以下内容: header('Content-type: application/json'); $theName = $_GET['name']; $theMessage = $_GET['message']; $str = file_get_contents("transactions.json"); if ($str == ""){ $str = '[]'; } $arr = json_decode($str, true); $arrne['

我使用JSON为聊天系统编写了以下内容:

header('Content-type: application/json');

$theName = $_GET['name'];
$theMessage = $_GET['message'];

$str = file_get_contents("transactions.json");

if ($str == ""){
     $str = '[]';
}

$arr = json_decode($str, true);

$arrne['name'] = "$theName";
$arrne['message'] = "$theMessage";

array_push($arr, $arrne);   

$aFinalTransaction = json_encode($arr, JSON_PRETTY_PRINT);
echo $aFinalTransaction;

file_put_contents("transactions.json", "$aFinalTransaction");
这将获取事务文件的内容,对其进行解码,插入从$\u GET获取的名称和消息,将其推送到数组中,并将其编码回字符串,然后将其放入事务文件中

这很好,但是我需要对XML而不是JSON做完全相同的事情。因此,文件中的字符串如下所示:

<chatMessage>
 <name>Name1</name>
 <message>Msg1</message>
</chatMessage>
<chatMessage>
 <name>Name2</name>
 <message>Msg2</message>
</chatMessage>
另外,还有一件事,我该如何准确地命名这个JSON对象?所以它看起来像这样:

{"messages":[
    {
        "name": "Name1",
        "message": "Msg1"
    },
    {
        "name": "Name2",
        "message": "Msg2"
    }
]}
最后一部分可能比我想象的要容易,但我没有任何运气


我真的希望你能帮助我。谢谢。

如果您想将消息放在该名称的元素下,只需确保数组遵循该格式即可。您也没有定义$arrne,您需要先将其设置为数组,您可以将其简化,如下所示

$str = file_get_contents( "transactions.json" );

// check if $str is a blank string, false, null, etc
if ( empty( $str ) ){
    // it's empty so create an array
    $arr = array();
} else {
    // it's not empty so decode the JSON string
    $arr = json_decode( $str, true );
}

// use isset() to avoid undefined index warnings
array_push( $arr, array(
    'name' => isset( $_GET['name'] )? $_GET['name'] : "",
    'message' => isset( $_GET['message'] )? $_GET['message'] : "",
) ); 

// assign to a new array with all the messages under the key "messages"
$result = array( 'messages' => $arr );

// convert array to JSON. see comment above for converting to XML with SimpleXML
$json = json_encode( $result, JSON_PRETTY_PRINT );

可能是复制品啊不错。事情是这样的;这是一个项目,我必须从一开始就把它写成XML,而不是JSON,然后转换成XML。你可以不用JSON就把PHP数组转换成XML。谢谢你的帮助。但这并不完全正确。运行多次编写的代码将使其看起来像这样3次{messages:{messages:{messages:[{name:name,message:S}],0:{name:name,message:S}},0:{name:name,message:S}}}此代码只是一个示例,它不支持在多个消息之间循环。
$str = file_get_contents( "transactions.json" );

// check if $str is a blank string, false, null, etc
if ( empty( $str ) ){
    // it's empty so create an array
    $arr = array();
} else {
    // it's not empty so decode the JSON string
    $arr = json_decode( $str, true );
}

// use isset() to avoid undefined index warnings
array_push( $arr, array(
    'name' => isset( $_GET['name'] )? $_GET['name'] : "",
    'message' => isset( $_GET['message'] )? $_GET['message'] : "",
) ); 

// assign to a new array with all the messages under the key "messages"
$result = array( 'messages' => $arr );

// convert array to JSON. see comment above for converting to XML with SimpleXML
$json = json_encode( $result, JSON_PRETTY_PRINT );