xmlfile to php array - php

i want to create an php array from an xml-file like this
test.xml
object(SimpleXMLElement)#3 (2) { ["uebertragung"]=> object(SimpleXMLElement)#5 (1) { ["#attributes"]=> array(9)
{ ["art"]=> string(7) "OFFLINE" ["umfang"]=> string(4) "TEIL"
["modus"]=> string(3) "NEW" ["version"]=> string(5) "1.2.7" ["sendersoftware"]=> string(7) "crm11" ["senderversion"]=> string(3) "1.1"
["techn_email"]=> string(18) "support#mail.com" ["timestamp"]=> string(19)
"2014-06-01T10:00:00" ["regi_id"]=> string(7) "ABCD143" } }
["anbieter"]=> object(SimpleXMLElement)#4 (3) { ["anbieternr"]=>
string(6) "144185" ["firma"]=> string(14) "redfirm"
["immobilie"]=> object(SimpleXMLElement)#6 (7) { ["objektkategorie"]=> object(SimpleXMLElement)#7 ...
mycode:
$xml = simplexml_load_file("test.xml") or die("Error: Cannot read xml file");
var_dump($xml);
echo "show1: " .$xml->openimmo->uebertragung->{'art'} . "<br>";
echo "show2: " .$xml->openimmo->uebertragung['art'];
show 1+2 show nothing, can someone help me, i dont understand the array structure ?

The art is an attribute, so you can get its value by
$xml->openimmo->uebertragung->attributes()->art
More here
http://php.net/manual/en/simplexmlelement.attributes.php

Related

How to parse steam api JSON and print it out? PHP

I want to parse JSON that I recieve from steam via api link http://steamcommunity.com/id/theorangeass/inventory/json/753/1/ , but when I try to print it with echo it show nothing.
Here is the code
$data = file_get_contents('http://steamcommunity.com/id/theorangeass/inventory/json/753/1/');
$json = json_decode($data, true);
echo $json->success;
var_dump:
array(6) { ["success"]=> bool(true) ["rgInventory"]=> array(1) { ["922506184369981039"]=> array(5) { ["id"]=> string(18) "922506184369981039" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["amount"]=> string(1) "1" ["pos"]=> int(1) } } ["rgCurrency"]=> array(0) { } ["rgDescriptions"]=> array(1) { ["1254492673_2070301907"]=> array(18) { ["appid"]=> string(3) "753" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["icon_url"]=> string(116) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiCJoFB3O541FNc9ZPYXYjjL7UqfFEwOtgZKcs0eWlClqzSJn6" ["icon_url_large"]=> string(106) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiA54UEGOnqGQPJ9hDZHA50feEo7RMyO_GQNzkkA" ["icon_drag_url"]=> string(0) "" ["name"]=> string(4) "BEEP" ["market_name"]=> string(0) "" ["name_color"]=> string(0) "" ["background_color"]=> string(0) "" ["type"]=> string(4) "Gift" ["tradable"]=> int(0) ["marketable"]=> int(0) ["commodity"]=> int(0) ["cache_expiration"]=> string(20) "2017-01-02T00:00:00Z" ["fraudwarnings"]=> array(1) { [0]=> string(223) "This is a restricted gift which can only be redeemed in these countries: Armenia, Azerbaijan, Belarus, Georgia, Kyrgyzstan, Kazakhstan, Moldova, Republic of, Tajikistan, Turkmenistan, Uzbekistan, Ukraine, Russian Federation" } ["descriptions"]=> array(1) { [0]=> array(1) { ["value"]=> string(216) "A combination of Yoshi’s Island-style platforming with a gravity gun right out of Half-Life, BEEP is an amazing physics-platformer. Despite its friendly art style, this is a hardcore platformer in the truest sense." } } ["actions"]=> array(1) { [0]=> array(2) { ["name"]=> string(13) "View in store" ["link"]=> string(41) "http://store.steampowered.com/app/104200/" } } } } ["more"]=> bool(false) ["more_start"]=> bool(false) }
You are trying to echo a boolean. If you are wanting to echo true you're going to need to do an if, or a switch statement. An if statement would probably be easiest.
echo $json->success == true ? 'TRUE' : 'FALSE';
To parse the array to retrieve the item id's in the rgInventory to must do a foreach.
foreach ($json['rgInventory'] as $item) {
echo $item['id'];
}
Where you should start learning about arrays is Here

How to refer to data in an array (php/json)

I am obtaining a json object using the following:
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
var_dump($data);
This gives me something like this:
array(2) { ["ok"]=> bool(true) ["result"]=> array(1) { [0]=> array(2)
{ ["update_id"]=> int(44893465) ["message"]=> array(5) {
["message_id"]=> int(16) ["from"]=> array(3) { ["id"]=> int(29595794)
["first_name"]=> string(3) "Bob" ["username"]=> string(14) "Bobo" }
["chat"]=> array(3) { ["id"]=> int(29595794) ["first_name"]=>
string(3) "Bob" ["username"]=> string(14) "Bobo" } ["date"]=>
int(1435354253) ["text"]=> string(7) "/q 3.33" } } } }
I would then like to add certain values into variables. For example I would like to extract username, text, message_id, etc
But no matter what I try my variables are empty:
//let's test it
echo "Username: " . $data[1][0]["username"];
//another test
echo $data->username;
Neither of these are working and my research has not helped me find a solution. I am stumped on this one.
Any pointers in the right direction would really be appreciated.
array(2) {
["ok"]=> bool(true)
["result"]=> array(1)
{
[0]=> array(2)
{
["update_id"]=> int(44893465)
["message"]=> array(5)
{
["message_id"]=> int(16)
["from"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["chat"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["date"]=> int(1435354253)
["text"]=> string(7) "/q 3.33"
}
}
}
}
You are using wrong array index. $data[1][0]["username"]; not exists.
$data["result"][0]["message"]["from"]["username"];
$data["result"][0]["message"]["chat"]["username"];
This will give you the proper username
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
echo $data["result"][0]['message']['from']['username'];
output
Bobo

simplexml_load_string - child nodes not showing

I am working with an xml file that I am trying to parse into json format and then decode to an array. I accomplished this mainly using the built in simplexml_load_string and then json_encode. The issue is when calling simplexml_load_string the xml isn’t fully preserved. It seems like the child nodes for video show as object(stdClass). How could I get all values of the xml file? Link to XML
Code:
$xml = simplexml_load_string( file_get_contents('http://foxsoccer2go.mobilefeeds.performgroup.com/fox/api/videos.xml/channel/home') );
$json = json_encode($xml);
Result:
["results"]=>
object(stdClass)#183 (4) {
["previousPage"]=>
object(stdClass)#184 (1) {
["#attributes"]=>
object(stdClass)#185 (1) {
["exists"]=>
string(5) "false"
}
}
["nextPage"]=>
string(1) "2"
["total"]=>
string(2) "40"
["resultList"]=>
object(stdClass)#186 (1) {
["video"]=>
array(20) {
[0]=>
object(stdClass)#187 (7) {
["#attributes"]=>
object(stdClass)#188 (2) {
["id"]=>
string(7) "2329124"
["type"]=>
string(3) "960"
}
["description"]=>
object(stdClass)#189 (0) {
}
["created"]=>
string(25) "2015-02-18 04:04:52 +0000"
["duration"]=>
string(2) "86"
["images"]=>
object(stdClass)#190 (2) {
["image"]=>
object(stdClass)#191 (1) {
["#attributes"]=>
object(stdClass)#192 (3) {
["id"]=>
string(8) "13503818"
["width"]=>
string(3) "100"
["height"]=>
string(3) "100"
}
}
["thumbnail"]=>
object(stdClass)#193 (1) {
["#attributes"]=>
object(stdClass)#194 (3) {
["id"]=>
string(8) "13503819"
["width"]=>
string(3) "372"
["height"]=>
string(3) "210"
}
}
}
["videoFiles"]=>
object(stdClass)#195 (1) {
["file"]=>
object(stdClass)#196 (1) {
["#attributes"]=>
object(stdClass)#197 (3) {
["id"]=>
string(8) "14704560"
["formatId"]=>
string(3) "400"
["uploaded"]=>
string(4) "true"
}
}
}
["categories"]=>
object(stdClass)#198 (1) {
["category"]=>
string(21) "UEFA Champions League"
}
}
I would suggest just try to parse those values using SimpleXML alone and stick with it. Just access those properties properly. As for those nodes which have been wrapped with character data in it, cast them as (string).
$xml = simplexml_load_string( file_get_contents('http://foxsoccer2go.mobilefeeds.performgroup.com/fox/api/videos.xml/channel/home'));
foreach($xml->results->resultList->video as $video) {
$description = (string) $video->description;
$created = $video->created;
$duration = $video->duration;
$image = $video->images->image;
$thumbnail = (string) $video->images->image;
$video_file = (string) $video->videoFiles->file;
$categories = (string) $video->categories->category;
echo "
Description: $description <br/>
Created: $created <br/>
Duration: $duration <br/>
Categories: $categories <br/>
<hr/>
";
}
Sample Output

JSON data parsing cannot understand

array(2) {
["success"]=>
string(1) "1"
["return"]=>
array(125) {
[0]=>
array(11) {
["marketid"]=>
string(3) "141"
["label"]=>
string(6) "42/BTC"
["primary_currency_code"]=>
string(2) "42"
["primary_currency_name"]=>
string(6) "42Coin"
["secondary_currency_code"]=>
string(3) "BTC"
["secondary_currency_name"]=>
string(7) "BitCoin"
["current_volume"]=>
string(10) "0.11628537"
["last_trade"]=>
string(12) "223.00000000"
["high_trade"]=>
string(12) "256.88999999"
["low_trade"]=>
string(12) "205.00000000"
["created"]=>
string(19) "2014-01-12 19:35:49"
}
}
I was trying to process this in PHP
$result = api_query("getmarkets");
$json = json_decode($result);
var_dump($result);
I tried many times to start processing this data but how would I start this
I was thinking something like $json['return'][0]['marketid'] would grab the market id.
Looks like it is already php object, so no need to parse as json.
try...
$result['return'][0]['marketid']
... and delete ...
$json = json_decode($result);

How to return data from an array within an array

I have the following data that I parsed from an xml file and now I have a problem returning the data. How do i return the photo src data from the array within the photo array. Any ideas as to what I am doing wrong?
Code
$xml = simplexml_load_file($url);
$photo_url = $xml['photo']->src;
for ($i=0, $n=count($photo_url); $i<$n; ++$i) {
echo $photo_url[$i].'<br>';
}
Data
["photo"]=>
array(46) {
[0]=>
object(SimpleXMLElement)#2 (1) {
["#attributes"]=>
array(6) {
["id"]=>
string(5) "26001"
["src"]=>
string(36) "1006416.jpg"
["thumb"]=>
string(42) "1006416_thumb.jpg"
["title"]=>
string(16) "album"
["subtitle"]=>
string(6) "01.jpg"
["favorite"]=>
string(0) ""
}
}
[1]=>
object(SimpleXMLElement)#3 (1) {
["#attributes"]=>
array(6) {
["id"]=>
string(5) "26001"
["src"]=>
string(36) "1006417.jpg"
["thumb"]=>
string(42) "1006417_thumb.jpg"
["title"]=>
string(16) "album"
["subtitle"]=>
string(6) "02.jpg"
["favorite"]=>
string(0) ""
}
}
You should use the SimpleXMLElement attributes method, like so:
$xml = simplexml_load_file($url);
foreach( $xml as $xml_node)
{
$attributes = $xml_node->attributes();
echo 'Photo source: ' . $attributes['src'] . "\n";
}
Demo

Categories