How to convert this tree data structure into an array - php

I used this tree data structure.
https://github.com/nicmart/Tree
and created a tree object from the following xml code which both are shown below:
<xgameapp version="1.0">
<states>
<state id="1" color="red"></state>
<state id="2" color="blue">
Hello state
</state>
</states>
<actions>
<transitions>
<transition from="1" to="2">
This is a demo Transition
</transition>
<transition from="2" to="1"></transition>
</transitions>
<Orientations>
<x1>
<y1>
<cw>
<Orientation fronAngle="120" toAngle="45"></Orientation>
<Orientation fronAngle="270" toAngle="120"></Orientation>
</cw>
<ccw>
<Orientation fronAngle="45" toAngle="120"></Orientation>
<Orientation fronAngle="120" toAngle="270"></Orientation>
</ccw>
</y1>
</x1>
</Orientations>
</actions>
</xgameapp>
Tree\Builder\NodeBuilder Object
(
[nodeStack:Tree\Builder\NodeBuilder:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => xgameapp
[attributes] => Array
(
[version] => 1.0
)
)
[parent:Tree\Node\Node:private] =>
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => states
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => state
[attributes] => Array
(
[id] => 1
[color] => red
)
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
[1] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => state
[attributes] => Array
(
[id] => 2
[color] => blue
)
[value] =>
Hello state
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
)
)
[1] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => actions
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => transitions
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => transition
[attributes] => Array
(
[from] => 1
[to] => 2
)
[value] =>
This is a demo Transition
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
[1] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => transition
[attributes] => Array
(
[from] => 2
[to] => 1
)
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
)
)
[1] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => Orientations
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => x1
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => y1
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => cw
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => Orientation
[attributes] => Array
(
[fronAngle] => 120
[toAngle] => 45
)
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
[1] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => Orientation
[attributes] => Array
(
[fronAngle] => 270
[toAngle] => 120
)
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
)
)
[1] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => ccw
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
[0] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => Orientation
[attributes] => Array
(
[fronAngle] => 45
[toAngle] => 120
)
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
[1] => Tree\Node\Node Object
(
[value:Tree\Node\Node:private] => Array
(
[name] => Orientation
[attributes] => Array
(
[fronAngle] => 120
[toAngle] => 270
)
)
[parent:Tree\Node\Node:private] => Tree\Node\Node Object
*RECURSION*
[children:Tree\Node\Node:private] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
Now I'm trying to represent this output as a nested array or any way that is friendly to the human eye.
Thanks.

Related

PHP - How to access this complex nbt node object?

Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 10 [name] => )
[parent:Nbt\Node:private] => [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object (
[parent:Nbt\Node:private] => [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object (
[value:Nbt\Node:private] => Array ( [type] => 9 [name] => i [payloadType] => 10 )
[parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private]
=> Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 2 [name] => id
[value] => 367 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION*
[children:Nbt\Node:private] => Array ( ) ) [1] => Nbt\Node Object ( [value:Nbt\Node:private] =>
Array ( [type] => 1 [name] => Count [value] => 64 ) <-- How to access this??[parent:Nbt\Node:private] => Nbt\Node Object
*RECURSION* [children:Nbt\Node:private] => Array ( ) ) [2] => Nbt\Node Object (
[value:Nbt\Node:private] => Array ( [type] => 10 [name] => tag ) [parent:Nbt\Node:private] =>
Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 9 [name] => ench [payloadType] => 0 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) [1] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 3 [name] => HideFlags [value] => 254 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) [2] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 10 [name] => display ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 9 [name] => Lore [payloadType] => 8 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [value] => �f�lCOMMON ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) [1] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 8 [name] => Name [value] => �fEnchanted Rotten Flesh ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) [3] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 10 [name] => ExtraAttributes ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 8 [name] => id [value] => ENCHANTED_ROTTEN_FLESH ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) ) ) [3] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 2 [name] => Damage [value] => 0 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) ) ) ) )
Alright I know this may look very messy and not organised but right now I need some help to access the Count value from this complicated nbt object as an echo-able string using PHP.
if you want a cleaner look of this here you go:
I'm so sorry if this looks messy but I can't find a way to format this crazy node nbt object thing I tried a lot of different ways but none of them work, Would really appreciate it if anyone helped.
Thanks.
$str = "...your big string... paste it here";
$needle = "Count [value] =>";
if(($pos = strpos($str, $needle)) !== false){
//echo "Count".substr($str,$pos+strlen($needle), )
// For precise numeric return -digit- might be very large, like, 100000000
// then
// find Count [value] => then find the first occurance of )
$newStr = substr($str, $pos+strlen($needle));
if(($posEnd = strpos($newStr, ")")) !== false){
echo "Count Value is ".trim(substr($newStr, 0, $posEnd));
}
}
output: Count Value is 64

SimpleXMLElement Object response for loop

Im trying to return a list of contacts from the Xero API however when getting the response back from there api i cannot run the array through a for each loop.
when trying to loop through nothing is displayed, has anyone had this problem in the past?
im trying to loop through to put each contact within a row inside a table.
if i run
pr($contacts->Contacts[0]);
it returns the list of contacts however it wont display anything with the for loop below
This is my for loop
foreach($contacts->Contacts[0] as $item) {
echo $item['Id'];
echo $item['filepath'];
}
this is the array returned
SimpleXMLElement Object
(
[Id] => 0dcda838-d354-4674-b6eb-d788e166b8dd
[Status] => OK
[ProviderName] => TestIntergration
[DateTimeUTC] => 2017-09-18T01:42:30.7361585Z
[Contacts] => SimpleXMLElement Object
(
[Contact] => Array
(
[0] => SimpleXMLElement Object
(
[ContactID] => 0ae2c0f4-cae9-4431-ab6b-52554f1c7c69
[ContactStatus] => ACTIVE
[Name] => Steve Buscemi
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-08-30T08:14:03.88
[IsSupplier] => false
[IsCustomer] => true
[HasAttachments] => false
)
[1] => SimpleXMLElement Object
(
[ContactID] => ab8a9286-740e-4246-876c-9fcac558d600
[ContactStatus] => ACTIVE
[Name] => Martin Hudson
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-08-30T08:20:03.263
[IsSupplier] => false
[IsCustomer] => true
[HasAttachments] => false
)
[2] => SimpleXMLElement Object
(
[ContactID] => 463e9f23-d1a1-4d74-b19e-041142c8fd34
[ContactStatus] => ACTIVE
[Name] => Orlena Greenville
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-09-01T07:56:32.667
[IsSupplier] => false
[IsCustomer] => false
[HasAttachments] => false
)
[3] => SimpleXMLElement Object
(
[ContactID] => b75bfe5b-cc6e-45ee-b0a1-feafe866088d
[ContactStatus] => ACTIVE
[Name] => asdfasdfasdf454
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-09-04T10:34:52.93
[IsSupplier] => false
[IsCustomer] => false
[HasAttachments] => false
)
)
)
)
Code used to get the passed data
$response = $XeroOAuth->request('GET', $XeroOAuth->url('Contacts', 'core'), array());
if ($XeroOAuth->response['code'] == 200) {
$contacts = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);
pr($contacts);
}

How to fetch data from array and insert to database

Hello i Have a problem related to array,i am trying to request graph api and saves data in database.here is my array
Array
(
[threadId] => t_mid.1445648572476:316f06a4adc6d7dd57
[sender] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[1] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[messages] => Array
(
[messages] => Array
(
[0] => Array
(
[message] => Same to u dear.
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-12-31T11:31:24+0000
[id] => m_mid.1451561484487:5b3276ddd9d86d1480
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => source:chat
)
)
)
)
[1] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T10:37:47+0000
[id] => m_mid.1451558267514:4c017e5a1f8e997586
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[2] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T09:44:49+0000
[id] => m_mid.1451555089630:1036078c7bcabeed78
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[3] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T09:43:28+0000
[id] => m_mid.1451555008621:62c3365e35ededc773
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[4] => Array
(
[message] => Very Good
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-09-01T09:13:34+0000
[id] => m_mid.1445648574720:c2746f8b55fff17369
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => source:chat
)
)
)
)
[5] => Array
(
[message] => Good
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-09-01T09:13:32+0000
[id] => m_mid.1445648572476:316f06a4adc6d7dd57
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => source:chat
)
)
)
)
)
)
)
This is my php code from that i fetch data
foreach($data as $evaluatemessage){
//echo "<pre>";print_r($evaluatemessage); die;
$inbox=array();
$inbox['thread_id']=$evaluatemessage['threadId'];
foreach($evaluatemessage['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
//$inbox['msg_type']=$data;
//$inbox['user_id']=$data;
$inbox['From_name']=$messagedet[$j]['from']->name;
$inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
$inbox['From_id']=$messagedet[$j]['from']->id;
$inbox['To_id']=$messagedet[$j]['to']->data[0]->id;
$inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
//$inbox['Page_id']=$data;
//echo "<pre>";print_r($inbox); die;
$inbox['Page_id']='12345';
$this->facebook_inboxes->save($inbox);
$j++;
}
$i++;
}
when i hit with this code only a single threadid and single message contents are saved.but rest are not,i want every thread id with there all messages id and message body are saved row by row but unable to do that can anyone help me how i do that.
Write this code before Save function
$this->facebook_inboxes->create();
This is enbuild function of cake to store multiple row.
There is another messages array inside the messages array. So according to your array structure, your second loop will be like this :
foreach($evaluatemessage['messages']['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
.
.
.
}
and you are using only:
foreach($evaluatemessage['messages'] as $messagedet){}
here is the full code :
foreach($data as $evaluatemessage){
//echo "<pre>";print_r($evaluatemessage); die;
$inbox=array();
$inbox['thread_id']=$evaluatemessage['threadId'];
foreach($evaluatemessage['messages']['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
//$inbox['msg_type']=$data;
//$inbox['user_id']=$data;
$inbox['From_name']=$messagedet[$j]['from']->name;
$inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
$inbox['From_id']=$messagedet[$j]['from']->id;
$inbox['To_id']=$messagedet[$j]['to']->data[0]->id;
$inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
//$inbox['Page_id']=$data;
//echo "<pre>";print_r($inbox); die;
$inbox['Page_id']='12345';
$this->facebook_inboxes->save($inbox);
$j++;
}
$i++;
}

Multidimensional array in PHP?

How to read specific values from multi dimensional array using php below is the array. I want to read values of [itemBody][div][0][p].
SimpleXMLElement Object
(
[#attributes] => Array
(
[toolName] => Eqiat
[toolVersion] => 0.7~git
[adaptive] => false
[timeDependent] => false
[identifier] => ITEM_b95e4391e33aa28561b01493b6e328f8
[title] => match the following
)
[stylesheet] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://gauss.ecs.soton.ac.uk/eqiat/eqiat.css
[type] => text/css
[title] => Eqiat item styles, designed to override QTIEngine's output where appropriate
)
)
[responseDeclaration] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_0_option_3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_1_option_2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_2_option_1
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_3_option_0
)
)
)
[outcomeDeclaration] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
[cardinality] => single
[baseType] => integer
)
[defaultValue] => SimpleXMLElement Object
(
[value] => 0
)
)
[itemBody] => SimpleXMLElement Object
(
[div] => Array
(
[0] => SimpleXMLElement Object
(
[p] => Match The Following
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => eqiat-emi
)
[ol] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => emioptions
)
[li] => Array
(
[0] => bangalore
[1] => india
[2] => tiger
[3] => onion
)
)
[choiceInteraction] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_0
)
[prompt] => vegetable
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_1
)
[prompt] => animal
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_2
)
[prompt] => country
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_3
)
[prompt] => city
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
)
)
)
)
[responseProcessing] => SimpleXMLElement Object
(
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[baseValue] => 0
)
[responseCondition] => Array
(
[0] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[1] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[2] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[3] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
)
)
)
Try:
$element = $object->itemBody->div->0->p;
That is an object and itemBody is a property that holds another object with a property div which is an array of objects with properties, p being one for the object at array index 0:
$result = $object->itemBody->div[0]->p;
First convert the complete object in to array and then access
' function object2array($object) { '
return json_decode(json_encode($object), true);
}
$data = object2array(simplexml_load_string($xml1));
echo '',print_r($data),'';`
Now i can access by echo $data[itemBody][div][0][p];

How to loop through an array of SimpleXMLElement objects and return node default value?

I am trying to get default value for each field but I just can't figure out how to loop through all the objects. Tried with converting them to a simple array with json_decode but it just not clear as to what to loop.
Here it is:
SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => params
)
[fieldset] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Cat1
)
[field] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] =>
[type] => list
[default] => 1
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Item2
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post1
[type] => text
[default] => 5
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post2
[type] => text
[default] => 18
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post3
[type] => text
[default] => 15
)
[option] => Array
(
[0] => Blue
[1] => Green
)
)
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Cat2
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post6
[type] => text
[default] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post7
[type] => text
[default] => 36
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => post7
[type] => text
[default] => 88
)
)
)
)
)
Try this (where $xml is your root object)
foreach($xml->fieldset as $fieldset) {
foreach($fieldset->field as $field) echo (string)$field['default'];
}

Categories