This is the print out from the variable $baseTypeDerivedDataTypeRefModel and is as far as I've got with getting the value I need. I need to extract the value int16, int64 etc from the SimpleXMLObject I've used $baseTypeDerivedDataTypeRefModel[0] and
$baseTypeDerivedDataTypeRefModel->getName() and a bunch of other stuff and cant get it to work, I know its probably something simple but its driving me crazy!
SimpleXMLElement Object ( [int16] => SimpleXMLElement Object ( [range] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) [ranges] => SimpleXMLElement Object ( [subrange] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) ) [multiplicationFactor] => 2 [resolution] => 2 ) )
SimpleXMLElement Object ( [int64] => SimpleXMLElement Object ( [range] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) [ranges] => SimpleXMLElement Object ( [subrange] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) ) [multiplicationFactor] => 2 [resolution] => 2 ) )
SimpleXMLElement Object ( [int16] => SimpleXMLElement Object ( [range] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) [ranges] => SimpleXMLElement Object ( [subrange] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) ) [multiplicationFactor] => 2 [resolution] => 2 ) )
SimpleXMLElement Object ( [uint8] => SimpleXMLElement Object ( ) )
SimpleXMLElement Object ( [int16] => SimpleXMLElement Object ( [range] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) [ranges] => SimpleXMLElement Object ( [subrange] => SimpleXMLElement Object ( [min] => 1 [max] => 10 ) ) [multiplicationFactor] => 2 [resolution] => 2 ) )
SimpleXMLElement Object ( [string] => SimpleXMLElement Object ( ) )
<derivedDataType name="DerivedIntDatatypeU16">
<description>Random Desc </description>
<baseType>
<int16>
<range>
<min>1</min>
<max>10</max>
</range>
<ranges>
<subrange>
<min>1</min>
<max>10</max>
</subrange>
</ranges>
<multiplicationFactor>2</multiplicationFactor>
<resolution>2</resolution>
</int16>
</baseType>
</derivedDataType>
Since you never know how many items "baseType" has (it is XML, after all), you could do this to get the name of the first item:
$xmlstring = '<derivedDataType name="DerivedIntDatatypeU16"> ... </derivedDataType>';
$xml = new SimpleXMLElement($xmlstring);
// Cast the SimpleXMLObject as an array
$list = (array) $data->baseType;
// Reset the array pointer (so we know we're at the start of the array)
reset($list);
// Get the key name of the first element
$name = key($list);
This fetches the baseType list, casts it as an array and then gets the key of the first array element.
There might be (or most likely are) better ways to do this, but it works.
Related
I have a xml and could get the arrays with the data I need with xpath after loaded the xml with simplexml_load_file.
I tried it this way : Access #attributes data in SimpleXMLElement in PHP
with my XML to array I still cant access the nodes, could someone please check my Code: thanks
$result2 = $xml->xpath("//file[#Catid='151']");
that is giving this array:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[path] => export/freexml.int/DE/4757.xml
[Product_ID] => 4757
[Updated] => 20170902053143
[Quality] => ICECAT
[Supplier_id] => 3
[Prod_ID] => TT34MUK
[Catid] => 151
[On_Market] => 1
[Model_Name] => THINKPAD T23 P3-1.13G
[Product_View] => 12655
[HighPic] => http://images.icecat.biz/img/norm/high/4757-6880.jpg
[HighPicSize] => 4138
[HighPicWidth] => 200
[HighPicHeight] => 150
[Date_Added] => 20050715000000
[Limited] => No
)
[EAN_UPCS] => SimpleXMLElement Object
(
[EAN_UPC] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Value] => 3606503209062
[IsApproved] => 0
)
)
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Value] => LU
)
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[path] => export/freexml.int/DE/41895.xml
[Product_ID] => 41895
[Updated] => 20170902052843
[Quality] => ICECAT
[Supplier_id] => 7
[Prod_ID] => LX.T2606.067
[Catid] => 151
[On_Market] => 1
[Model_Name] => TRAVELMATE 432LC P4-2.53G
[Product_View] => 12056
[HighPic] => http://images.icecat.biz/img/norm/high/41895-65.jpg
[HighPicSize] => 14404
[HighPicWidth] => 330
[HighPicHeight] => 290
[Date_Added] => 20050715000000
[Limited] => No
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Value] => DE
)
)
)
)
How can I access the values like 'path' and so on? I have problems with the
[0] => SimpleXMLElement Object so what is the Name of the node?
echo (string)$result2->0[0]->attributes()->path;
didnt work.....
thanks
Although it would be easier to check with the XML, you will probably find the correct notation is...
echo (string)$result2[0]->attributes()->path;
This assumes that you want the first item that XPath has found.
You may also find
echo (string)$result2[0]['path'];
Works (but not always).
What I want to do is read the information and be able to get the data of each game, so which teams played, who won and etc. I've tried everything but can't seem to do this.
This is my data structure:
stdClass Object (
[_links] => Array (
[0] => stdClass Object (
[self] => http://api.football-data.org/alpha/soccerseasons/354/fixtures
)
[1] => stdClass Object (
[soccerseason] => http://api.football-data.org/alpha/soccerseasons/354
)
)
[count] => 10
[fixtures] => Array (
[0] => stdClass Object (
[_links] => stdClass Object (
[self] => stdClass Object (
[href] => http://api.football-data.org/alpha/fixtures/137842
)
[soccerseason] => stdClass Object (
[href] => http://api.football-data.org/alpha/soccerseasons/354
)
[homeTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/338
)
[awayTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/70
)
)
[date] => 2015-01-17T15:00:00Z
[status] => FINISHED
[matchday] => 22
[homeTeamName] => Leicester City
[awayTeamName] => Stoke City FC
[result] => stdClass Object (
[goalsHomeTeam] => 0
[goalsAwayTeam] => 1
)
)
[1] => stdClass Object (
[_links] => stdClass Object (
[self] => stdClass Object (
[href] => http://api.football-data.org/alpha/fixtures/136840
)
[soccerseason] => stdClass Object (
[href] => http://api.football-data.org/alpha/soccerseasons/354
)
[homeTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/72
)
[awayTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/61
)
)
[date] => 2015-01-17T15:00:00Z
[status] => FINISHED
[matchday] => 22
[homeTeamName] => Swansea City
[awayTeamName] => Chelsea FC
[result] => stdClass Object (
[goalsHomeTeam] => 0
[goalsAwayTeam] => 5
)
)
)
)
For example: I'd like to know what the value for "homeTeamName","awayTeamName", "goalsHomeTeam", "goalsAwayTeam" ...
Its hard to tell because you didnt post the code you are using but i suspect you are confusing the structure type. What you posted is actually an object - an instance of stdClass and the items that look like array elements are in fact properties on that object. So you need to use the appropriate access method of ->propertyName as opposed to ['propertyName']:
// YES
echo $data->fixtures[0]->homeTeamName;
// NO
echo $data['fixtures'][0]['homeTeamName'];
So to get the data for each game (or what i presume to be the "game" items) you would do:
foreach ($data->fixtures as $game) {
echo $game->homeTeamName;
echo $game->awayTeamName;
echo $game->result->goalsHomeTeam;
echo $game->result->goalsAwayTeam;
// etc..
}
It also seems like this is the return value from an API parsed with json_decode(). If that is the case you can actually make it an array all the way through by passing true as the second argument to json_decode():
$data = json_decode($response, true);
foreach ($data['fixtures'] as $game) {
echo $game['homeTeamName'];
echo $game['awayTeamName'];
echo $game['result']['goalsHomeTeam'];
echo $game['result']['goalsAwayTeam'];
// etc..
}
I have seen so many example at stackoverflow to sort multidimensional array of object like
Sort array of objects
Sort array of objects by object fields
But none them could help me to sort my multidimensional array of objects given below
SimpleXMLElement Object
(
[request] => SimpleXMLElement Object
(
[address] => test
[citystatezip] => New York
)
[message] => SimpleXMLElement Object
(
[text] => Request successfully processed
[code] => 0
)
[response] => SimpleXMLElement Object
(
[results] => SimpleXMLElement Object
(
[result] => Array
(
[0] => SimpleXMLElement Object
(
[zpid] => 27965224
[links] => SimpleXMLElement Object
(
[homedetails] => test
[graphsanddata] =>test
[mapthishome] => test
[comparables] => test
)
[address] => SimpleXMLElement Object
(
[street] => test
[zipcode] => test
[city] => test
[state] => NY
[latitude] => 29.802114
[longitude] => -95.504244
)
[zestimate] => SimpleXMLElement Object
(
[amount] => 342911
[last-updated] => 11/27/2014
[oneWeekChange] => SimpleXMLElement Object
(
[#attributes] => Array
(
[deprecated] => true
)
)
[valueChange] => 5766
[valuationRange] => SimpleXMLElement Object
(
[low] => 312049
[high] => 373773
)
[percentile] => 0
)
[rentzestimate] => SimpleXMLElement Object
(
[amount] => 5177
[last-updated] => 11/24/2014
[oneWeekChange] => SimpleXMLElement Object
(
[#attributes] => Array
(
[deprecated] => true
)
)
[valueChange] => 370
[valuationRange] => SimpleXMLElement Object
(
[low] => 3417
[high] => 7041
)
)
[localRealEstate] => SimpleXMLElement Object
(
[region] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 271582
[type] => neighborhood
[name] => test
)
[links] => SimpleXMLElement Object
(
[overview] => test
[forSaleByOwner] => test
[forSale] => test
)
)
)
)
[1] => SimpleXMLElement Object
[2] => SimpleXMLElement Object
[3] => SimpleXMLElement Object
..............................
..............................
)
)
)
)
I need to sort the above array on the basis of key amount in descending order. But the problem is amount key exist under two different parent keys "zestimate" and "rentzestimate".
i have tried the following function but it did not work:
public function my_comparison($a, $b) {
if ($a->amount == $b->amount) {
return 0;
}
return ($a->amount < $b->amount) ? -1 : 1;
}
Any help?
Thanks in advance
response->results->result is an array of SimpleXMLElement objects. You want to sort the array based on the inner zestimate->amount property of the element in descending order.
You have to write a comparison function that accepts SimpleXMLElement objects and, because you want a descending order, returns 1 if the zestimate->amount property of the first object is less than that of the second, -1 if it's greater and 0 if it's equal:
public function my_comparison(SimpleXMLElement $a, SimpleXMLElement $b) {
if ($a->zestimate->amount == $b->zestimate->amount) {
return 0;
}
return ($a->zestimate->amount < $b->zestimate->amount) ? 1 : -1; // note the signs
}
I need to loop through the items as an array within the SimpleXMLElement Object below but cannot seem to access it using $order->order->order->items. I can access the delivery and billing addresses using the same format, ie. $order->order->order->delivery_address and expected to get to the items array in the same way. However, I get an empty SimpleXMLElement Object when I print_r($order->order->order->items)
SimpleXMLElement Object
(
[order] => SimpleXMLElement Object
(
[id] => 860268
[shopkeeper_orderno] => 1001
[customer] => 797476
[creationdate] => Apr 19 2012 10:36:38:100AM
[reference] => k2koju45rmaqfl45n20xbkmq
[net] => 1500
[vat] => 17.5
[status] => 0
[isnew] => 1
[deductions] => 0
[postage] => 1
[paymentmethod] => PayPal
[instructions] => SimpleXMLElement Object
(
)
[errors] => SimpleXMLElement Object
(
)
[kashflow_synch] => 0
[order] => Array
(
[0] => SimpleXMLElement Object
(
[billing_address] => SimpleXMLElement Object
(
[0] =>
)
)
[1] => SimpleXMLElement Object
(
[delivery_address] => SimpleXMLElement Object
(
[0] =>
)
)
[2] => SimpleXMLElement Object
(
[items] => Array
(
[0] => SimpleXMLElement Object
(
[id] => 1285158
[headerID] => 860268
[productID] => 4867690
[description] => TEST ORDERING PF NODES - Special Offer Price
[net] => 1400
[vat] => 0
[qty] => 1
[formID] => -1
)
[1] => SimpleXMLElement Object
(
[id] => 1285159
[headerID] => 860268
[productID] => 4959678
[description] => Wedding dress
[net] => 100
[vat] => 17.5
[qty] => 1
[formID] => -1
)
)
)
)
[postage_tax] => 0
[dispatched] => 0
[paybyotherid] => -1
[ip] => 81.168.43.121
[wheredidyouhearid] => -1
)
)
EDIT: I just saw you made a mistake with the naming, the parent needs to be called <orders> and the sub items <order>
The SimpleXMLElement seems to be empty, in fact it's usually filled but not displayed when dumping (whoever thought of this crazy behaviour)
Can you try this?
foreach($order->orders->order as $order) { // should be orders then
echo $item->getName();
}
Or try it with SimpleXMLElement::children()
your items are actually on the second offset of the order array.
I'd just use the xPath to process these.
foreach($xmlObject->xpath('/order/order[2]/items') as $item)
{
// Do something with my $item
}
You can use a loop like the one below, then all you need to do is $items->id
foreach($order->children()->children()->items as $items)
{
}
Using Dan Lees suggestion I tried SimpleXMLElement::children() and did the below which works
foreach ($order->children() as $order) {
foreach ($order->children() as $order_details) {
foreach ($order_details->children() as $order_items) {
echo $order_items->id;
}
}
}
I have a XML which I parse with php simpleXML.
The XML:
<GetOneGetAll DateTimeSystem="28-06-2011 17:19:29" RetCode="200" RetVal="1" RetMsg="User ok.">
<User Id="bc5cb4cf-19a6-4504-8e1a-f72dd97bcc66" ReferedConfirmedUsers="0" TotalRecomendations="0" DistinctRecomendations="0">
<Name>Name</Name>
<Surname>Surname</Surname>
<Gender>F</Gender>
<Email>email#email.com</Email>
<RefererCode>59286904</RefererCode>
<CustomPhotoMessage HasCustomPhoto="0" HasCustomMessage="0"/>
<ReferedConfirmedUsersList/>
</User>
</GetOneGetAll>
When I print_r the var using simpleXML I get:
SimpleXMLElement Object
(
[#attributes] => Array
(
[DateTimeSystem] => 28-06-2011 17:22:52
[RetCode] => 200
[RetVal] => 1
[RetMsg] => Login ok.
)
[User] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Id] => bc5cb4cf-19a6-4504-8e1a-f72dd97bcc66
[ReferedConfirmedUsers] => 0
[TotalRecomendations] => 0
[DistinctRecomendations] => 0
)
[Name] => SimpleXMLElement Object
(
)
[Surname] => SimpleXMLElement Object
(
)
[Gender] => SimpleXMLElement Object
(
)
[Email] => SimpleXMLElement Object
(
)
[RefererCode] => SimpleXMLElement Object
(
)
[CustomPhotoMessage] => SimpleXMLElement Object
(
[#attributes] => Array
(
[HasCustomPhoto] => 0
[HasCustomMessage] => 0
)
)
[ReferedConfirmedUsersList] => SimpleXMLElement Object
(
)
)
)
Where is the data of Surname, Name, Email, Gender, etc.?
This is just a guess, but if you have xdebug installed, then the default recursion level of var_dump output is 3. This setting is xdebug.var_display_max_depth
You are using print_r, but some similar recursive limit could be being reached.