Reputation: 167
SimpleXMLElement Object
(
[@attributes] => Array
(
[version] => 2.0
)
[channel] => SimpleXMLElement Object
(
[title] => Yahoo!ニュース・トピックス - トップ
[link] => http://news.yahoo.co.jp/
[description] => aa
[language] => ja
[pubDate] => Mon, 17 Aug 2015 10:20:57 +0900
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => aa
[link] => aa
[pubDate] => aa
[enclosure] => SimpleXMLElement Object
(
[@attributes] => Array
(
[length] => 133
[url] => http://i.yimg.jp/images/icon/photo.gif
[type] => image/gif
)
[0] =>
)
[guid] => yahoo/news/topics/6170952
)
[1] => SimpleXMLElement Object
(
[title] => bb
[link] => bb
[pubDate] => bb
[enclosure] => SimpleXMLElement Object
(
[@attributes] => Array
(
[length] => 133
[url] => http://i.yimg.jp/images/icon/photo.gif
[type] => image/gif
)
[0] =>
)
[guid] => yahoo/news/topics/6170951
)
I've got this array which is very confusing for me as a beginner.
I just want to put the title, link and pubDate
from 0=> SimpleXMLELement Object
and 1 => SimpleXMLElement Object
which has aa
and bb
in them into a my table pulled from mysql.
My table goes like:
Title
PubDate
Link
and I want to put the 'aa' and 'bb' under each of the titles in the table.
This is what I have tried:
foreach($con as $key => $val){
while($key == title){
$Title['title'] = $val;
}
return $Title['title'];
}
What I was trying to do was label the keys and values with $key
and $val
, and when $key = title
, I wanted to put all the titles into one array, but unfortunately it did not work.
Upvotes: 1
Views: 69
Reputation: 6386
Actually that array is not complicated at all once you start to understand what is going on and how its structured it will continue to become a piece of cake over time. Instead of me showing you what to do i'm going to explain in detail how to read an array like that and how to go about it and that way you will know what to do in the future.
Example, lets say you want to access the version value in this portion of the array:
SimpleXMLElement Object
(
[@attributes] => Array
(
[version] => 2.0
)
)
First you would have this stored in a variable and each level under the array is setup like this and say this array is stored in $container
//with @attributes you access it as a function, anything with @ is access as a function, example below.
$version = $container->attributes()->['version']
now say you want to access the title, link, pubdate etc.
you would do
$title = $container->channel->title;
$link = $container->channel->link;
$description = $container->channel->description
//notice how after channel i used it as an object instead of an array , its because after channel it mentions that its an SimpleXMLElement Object.
Now say you want to access each of the elements under item where it has values starting from 0.
$collection = $container->channel->item;
now this holds all the elements of item. To prove it simply do print_r($collection) and you will see a collection of data returned to you and to get an output of its collection you can just do a simple foreach loop to get all the values, as shown:
foreach($collection as $items)
{
echo 'Title: ' . $items->title . '<br/>';
echo 'Link: ' . $items->link . '<br/>';
echo 'Pubdate' . $items->pubDate . '<br/>';
//and so on
}
Once you understand this , then you can create your table or whatever else you have to do but you must first understand how to read arrays like this.
Upvotes: 1
Reputation: 3407
Try this:
$array_to_get = ['title', 'link', 'pubDate'];
$newArray = [];
$con = $xmlStructure->channel->item; //Object from xml
foreach($con as $key => $val){
if(in_array($key, $array_to_get)){
$newArray['title'] = $val->title;
$newArray['link'] = $val->link;
$newArray['pubDate'] = $val->pubDate;
}
}
print_r($newArray);
Upvotes: 2
Reputation: 204
Assuming that $xmlStructure is the structure posted above, your code should look something like:
$items = $xmlStructure->channel->item;
$table = [];
foreach ($items as $item) {
$table[] = [
'title' => $item->title,
'pubDate' => $item->pubDate,
'link' => $item->link;
];
}
At the end of this, your $table array will contain an array of entities, each containing the three fields.
Then, constructing an HTML table (or table of another type) should be fairly straightforward.
Upvotes: 1