Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将xml对象推入数组_Php_Xml_Arrays - Fatal编程技术网

Php 将xml对象推入数组

Php 将xml对象推入数组,php,xml,arrays,Php,Xml,Arrays,好的,所以我对这个有点困惑。我有一个xml,看起来像这样: <track> <artist mbid="77cceea7-91bb-4a4c-ae41-bc9c46c1ccb5"> Red Hot Chili Peppers </artist> <name> under the bridge </name> <streamable>0</streamable> <mbid/> <album mb

好的,所以我对这个有点困惑。我有一个xml,看起来像这样:

<track>
<artist mbid="77cceea7-91bb-4a4c-ae41-bc9c46c1ccb5"> Red Hot Chili Peppers </artist>
<name> under the bridge </name>
<streamable>0</streamable>
<mbid/>
<album mbid="0fe94139-df63-4e51-b2e7-a1d53535cdd9">  Blood Sugar Sex Magik </album>
<url> http://xxxxxx.com </url>
<date uts="1351691244">31 Oct 2012, 13:47</date>
</track>
$artists = array();

$xml = simplexml_load_file("http://xxxxxxxxxxxxxx");

foreach($xml->recenttracks->track  as $track)
{
$artist = $track->artist;
    array_push($artists, $artist);
}  

var_dump($artists);
array(4) {
[0]=>
string(20) "Red Hot Chili Peppers "
[1]=>
string(20) "Red Hot Chili Peppers"
}
array(2) 
{ 
[0]=> object(SimpleXMLElement)#6 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
[1]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
} 
现在我希望得到一个像这样的漂亮阵列:

<track>
<artist mbid="77cceea7-91bb-4a4c-ae41-bc9c46c1ccb5"> Red Hot Chili Peppers </artist>
<name> under the bridge </name>
<streamable>0</streamable>
<mbid/>
<album mbid="0fe94139-df63-4e51-b2e7-a1d53535cdd9">  Blood Sugar Sex Magik </album>
<url> http://xxxxxx.com </url>
<date uts="1351691244">31 Oct 2012, 13:47</date>
</track>
$artists = array();

$xml = simplexml_load_file("http://xxxxxxxxxxxxxx");

foreach($xml->recenttracks->track  as $track)
{
$artist = $track->artist;
    array_push($artists, $artist);
}  

var_dump($artists);
array(4) {
[0]=>
string(20) "Red Hot Chili Peppers "
[1]=>
string(20) "Red Hot Chili Peppers"
}
array(2) 
{ 
[0]=> object(SimpleXMLElement)#6 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
[1]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
} 
但我得到的是这样的东西:

<track>
<artist mbid="77cceea7-91bb-4a4c-ae41-bc9c46c1ccb5"> Red Hot Chili Peppers </artist>
<name> under the bridge </name>
<streamable>0</streamable>
<mbid/>
<album mbid="0fe94139-df63-4e51-b2e7-a1d53535cdd9">  Blood Sugar Sex Magik </album>
<url> http://xxxxxx.com </url>
<date uts="1351691244">31 Oct 2012, 13:47</date>
</track>
$artists = array();

$xml = simplexml_load_file("http://xxxxxxxxxxxxxx");

foreach($xml->recenttracks->track  as $track)
{
$artist = $track->artist;
    array_push($artists, $artist);
}  

var_dump($artists);
array(4) {
[0]=>
string(20) "Red Hot Chili Peppers "
[1]=>
string(20) "Red Hot Chili Peppers"
}
array(2) 
{ 
[0]=> object(SimpleXMLElement)#6 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
[1]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
} 

现在,我如何仅获取艺术家,而不是整个SimpleXMLElement,因为我无法理解它。

var\u dump提供了可以用于访问不同片段的键,而强制转换到字符串将只提供节点值,请尝试:

$artist = (string) $track->artist[0];

看看这些链接。。。他们过去帮助过我

请尝试下面的代码

$xml = simplexml_load_file("artist.xml");

echo $xml->artist;

要添加到数组中的项是
simplexmlement
s。如果只想添加字符串值,则必须将
simplexmlement
强制转换为字符串

$artists = array();
foreach($xml->recenttracks->track  as $track)
{
    $artists[] = (string) $track->artist;
}  

var_export($artists);
通常,当需要字符串值时,总是希望将
simplexmlement
强制转换为字符串。在某些情况下,PHP会自动强制为字符串(例如,当您
echo
it时),但PHP类型强制规则非常复杂,最好始终是显式的


(另外,不需要
array_push()
,只需使用括号符号
$arrayname[]=$appendedvalue

如果我只回显艺术家,我只会得到艺术家的名字,但如果我把它推到数组中,我会得到所有这些额外的信息。在描述中忘了提到这一点。@user1593846-令人困惑的是,您没有发布完整的XML文档,只有一个节点。有关如何访问该值,请参见我的答案。