I have a problem with the follow php object dump. I try to check if "price" is an array, php return me always false. var_dump() of array "price" return me first element of array and not the entire array. Which is wrong? How can I check if it is an array?
object(SimpleXMLElement)#197 (1) {
["price"]=> array(4) {
[0]=> object(SimpleXMLElement)#156 (4) {
["room_id"]=> string(4) "1755"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(4) "5842"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(4) "2450"
}
}
[1]=> object(SimpleXMLElement)#143 (4) {
["room_id"]=> string(5) "24206"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(4) "5842"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(4) "2450"
}
}
[2]=> object(SimpleXMLElement)#135 (4) {
["room_id"]=> string(4) "1755"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(6) "415725"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(6) "2327.5"
}
}
[3]=> object(SimpleXMLElement)#136 (4) {
["room_id"]=> string(5) "24206"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(6) "415725"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(6) "2327.5"
}
}
}
}
Consider a simplified example:
<root>
<element>first</element>
<element>second</element>
<element>third</element>
</root>
$root->element is not really an array. It is SimpleXMLElement object. You can think of it as a selector, a collection of elements you can traverse using foreach and also access specific object using indexes:
$root->element[0]; //first object
$root->element[1]; //second
$root->element[2]; //third
Here is more examples of basic usage: http://php.net/manual/en/simplexml.examples-basic.php
So the real question is: How to define if there is more than one element in collection ?
You can do it using count() method:
if($es->result->hotel->channel->room_price->price->count() > 1){
echo 'many elements';
}
Read more: http://php.net/manual/en/simplexmlelement.count.php
// simplexml_load_string() return false if not $XML
$xml = simplexml_load_string($pSdata);
if(gettype($xml)=='object')
{
print_r(simplexmlArray($xml));
} else {
echo "no xml input";
}
// simplexml_load $xml obj to array
function simplexmlArray ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? simplexmlArray ( $node ) : $node;
return $out;
}
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
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
Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>
:) Let's say that i have that code:
<sample number="1">TEXT</sample>
but sometimes it could be
<sample number"1"/>
Q: How to check if it's self closed or not ? Or I want to check if it's there TEXT within element sample
Note: I'm using that way to retrieve XML doc:
$content = #file_get_contents($url);
$xml = new SimpleXMLElement($content);
You need to type cast the element to string, then check if it's empty or not.
Here's a quick example:
$test = simplexml_load_string("<test><elem test='12'><sub /><sub /></elem><elem test='12'>hi</elem><elem test='9' /><elem /></test>");
foreach($test as $elem){
echo "\n";
var_dump($elem);
if((string)$elem == '' && $elem->count() == 0)
echo 'Empty';
else
echo 'Full';
}
Will return:
object(SimpleXMLElement)#3 (2) {
["#attributes"]=>
array(1) {
["test"]=>
string(2) "12"
}
["sub"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#4 (0) {
}
[1]=>
object(SimpleXMLElement)#5 (0) {
}
}
}
Full
object(SimpleXMLElement)#5 (2) {
["#attributes"]=>
array(1) {
["test"]=>
string(2) "12"
}
[0]=>
string(2) "hi"
}
Full
object(SimpleXMLElement)#3 (1) {
["#attributes"]=>
array(1) {
["test"]=>
string(1) "9"
}
}
Empty
object(SimpleXMLElement)#5 (0) {
}
Empty