PHP、SimpleXML 数组。意外地将数组转换为字符串

发布于 2024-09-26 01:16:46 字数 1195 浏览 0 评论 0原文

示例 XML:

<root>
  <ratings>3</ratings>
  <ratings>5</ratings>
  <ratings>7</ratings>
</root>

以下代码是我的小型应用程序的基础,它按预期工作:

<?php 
   // $xml is some simplexml object
   sizeof($xml->ratings); //3
   foreach($xml->ratings as $rating){
       echo($rating->value."::"); //this echoes all 3 rating values: 3::5::7
}
?>

我通常认为等效的下一个代码并非如此。我不知道为什么:

<?php 
    // $xml is some simplexml object
   $ratings = $xml->ratings;
   sizeof($ratings); //3, all is well so far
   foreach($ratings as $rating){
      echo($rating."::"); 
     /*this echoes a never-ending list of ratings,
     looking like 3::5::5::5::5::5::5::5...... */
  }
?>

我的感觉是赋值运算符将 simplexml 对象(评级对象)数组转换为奇怪的东西,但不知道如何转换。

其他小提示:

var_dump($xml);
/* Output is:
object(SimpleXMLElement)#7 (1) {
  ["ratings"]=>
  array(3) {
    [0]=>
    string(1) "3"
    [1]=>
    string(1) "5"
    [2]=>
    string(1) "7"
  }
}
*/
var_dump($ratings);
/* Output is:
object(SimpleXMLElement)#6 (1) {
    [0]=>
    string(1) "3"
}
*/

Sample XML:

<root>
  <ratings>3</ratings>
  <ratings>5</ratings>
  <ratings>7</ratings>
</root>

The following code is the basis for my small application, it works as would be expected:

<?php 
   // $xml is some simplexml object
   sizeof($xml->ratings); //3
   foreach($xml->ratings as $rating){
       echo($rating->value."::"); //this echoes all 3 rating values: 3::5::7
}
?>

This next code, which I would normally consider to be equivalent is not. And I have no idea why:

<?php 
    // $xml is some simplexml object
   $ratings = $xml->ratings;
   sizeof($ratings); //3, all is well so far
   foreach($ratings as $rating){
      echo($rating."::"); 
     /*this echoes a never-ending list of ratings,
     looking like 3::5::5::5::5::5::5::5...... */
  }
?>

My feeling is that the assignment operator is casting the array of simplexml objects (ratings objects) as something odd, but have no clue as to how.

Other little hints:

var_dump($xml);
/* Output is:
object(SimpleXMLElement)#7 (1) {
  ["ratings"]=>
  array(3) {
    [0]=>
    string(1) "3"
    [1]=>
    string(1) "5"
    [2]=>
    string(1) "7"
  }
}
*/
var_dump($ratings);
/* Output is:
object(SimpleXMLElement)#6 (1) {
    [0]=>
    string(1) "3"
}
*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请恋爱 2024-10-03 01:16:46

您的 echo 不一样:

echo($rating."::"); 

应该是

echo($rating->value."::");

Your echos are not the same:

echo($rating."::"); 

should be

echo($rating->value."::");
夏末 2024-10-03 01:16:46

好的,清理一些我自己的工作。在尝试进一步简化我的问题后,我无法证明这一点。在搞乱了实际的代码之后,我认为这意味着我的应用程序中的其他地方有某种变异对象,这些对象会变得疯狂并在这个 xml 解析中创建奇怪的结果。对于造成的混乱和不必要的问题感到抱歉(我想这证明了为什么我试图从这个应用程序中重构一些复杂性)。

作为临别礼物,这里是我使用的代码测试套件(从简单到更实际),我用它来证明所有功能都按照广告宣传的那样工作:

<?php
$string = <<<XML
<?xml version='1.0'?> 
<root>
 <ratings>3</ratings>
 <ratings>5</ratings>
 <ratings>7</ratings>
</root>
XML;
$xml = simplexml_load_string($string);
var_dump($xml);

echo("Size:".sizeof($xml->ratings)."\n");
foreach($xml->ratings as $rating){
echo($rating."::");
}
echo("\n"."------"."\n");

$ratings = $xml->ratings;

echo("Size:".sizeof($ratings)."\n");
foreach($ratings as $rating){
echo($rating."::");
}

echo("\n\n\n\n"."||||New Example||||"."\n\n\n\n");

$stringthree = <<<XML
<root attr1="val" attr2="desc">
      <field-one>val</field-one>
      <elm-two attr-name="foo">elmTwoVal1</elm-two>
      <elm-three>elmThreeVal1</elm-three>
      <elm-two attr-name="bar">elmTwoVal2</elm-two>
      <elm-three>elmThreeVa2</elm-three>
      <elm-two attr-name="bear">elmTwoVal3</elm-two>
      <elm-three>elmThreeVal3</elm-three>
</root>
XML;

$xmlthree = simplexml_load_string($stringthree);

var_dump($xmlthree);
echo("Size:".sizeof($xmlthree->{'elm-two'})."\n");
foreach($xmlthree->{'elm-two'} as $elm){
echo($elm."::");
}
echo("\n"."------"."\n");

$elmMain = $xmlthree->{'elm-two'};
echo("Size:".sizeof($elmMain)."\n");
foreach($elmMain as $elm){
echo($elm."::");
}

?>

Ok, cleaning up some of my own work. After attempting to simplify my issue more, I was not able to prove it. After messing with the actual code, I assume this means I have some sort of mutating object elsewhere in my app that is going bonkers and creating weird results in this xml parsing. Sorry for the confusion and needless question (I guess this proves why i'm trying to refactor some of my complexity out of this app).

As a parting gift, here is the test-suite of code that I used (from simple to more realistic) that I used to prove that all worked as advertised:

<?php
$string = <<<XML
<?xml version='1.0'?> 
<root>
 <ratings>3</ratings>
 <ratings>5</ratings>
 <ratings>7</ratings>
</root>
XML;
$xml = simplexml_load_string($string);
var_dump($xml);

echo("Size:".sizeof($xml->ratings)."\n");
foreach($xml->ratings as $rating){
echo($rating."::");
}
echo("\n"."------"."\n");

$ratings = $xml->ratings;

echo("Size:".sizeof($ratings)."\n");
foreach($ratings as $rating){
echo($rating."::");
}

echo("\n\n\n\n"."||||New Example||||"."\n\n\n\n");

$stringthree = <<<XML
<root attr1="val" attr2="desc">
      <field-one>val</field-one>
      <elm-two attr-name="foo">elmTwoVal1</elm-two>
      <elm-three>elmThreeVal1</elm-three>
      <elm-two attr-name="bar">elmTwoVal2</elm-two>
      <elm-three>elmThreeVa2</elm-three>
      <elm-two attr-name="bear">elmTwoVal3</elm-two>
      <elm-three>elmThreeVal3</elm-three>
</root>
XML;

$xmlthree = simplexml_load_string($stringthree);

var_dump($xmlthree);
echo("Size:".sizeof($xmlthree->{'elm-two'})."\n");
foreach($xmlthree->{'elm-two'} as $elm){
echo($elm."::");
}
echo("\n"."------"."\n");

$elmMain = $xmlthree->{'elm-two'};
echo("Size:".sizeof($elmMain)."\n");
foreach($elmMain as $elm){
echo($elm."::");
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文