Multidimensional array in PHP? - 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];

Related

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);
}

Access array returned from xpath in PHP

How can I access (in order to display) the elements returned by xpath?
My xpath call is
$products = $this->_dom->xpath('//menu/category[#name="'.$category.'"]');
and what I got is
Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Tomato and Cheese ) [type] =>
Regular [available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Small ) [price] => 5.50 ) [1] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Large ) [price] => 9.75 ) ) ) ) [1] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [#attributes]
=> Array ( [name] => Pepperoni ) [type] => Regular [available] => true [size] => Array ( [0]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Small ) [price] => 6.85 ) [1]
=> SimpleXMLElement Object ( [#attributes] => Array ( [name] => Large ) [price] => 10.85 ) ) )
) [2] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => Meatball ) [type] => Regular
[available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array
( [name] => Small ) [price] => 6.85 ) [1] => SimpleXMLElement Object ( [#attributes] => Array
( [name] => Large ) [price] => 10.85 ) ) ) ) [3] => SimpleXMLElement Object ( [#attributes] =>
Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [#attributes] => Array ( [name]
=> Hawaiian ) [type] => Regular [available] => true [size] => Array ( [0] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => Small ) [price] => 7.95 ) [1] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => Large ) [price] => 11.80 ) ) ) ) [4] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => pizza ) [item] =>
SimpleXMLElement Object ( [#attributes] => Array ( [name] => Three Aces Special ) [type] =>
Speciality [available] => true [size] => Array ( [0] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Small ) [price] => 9.80 ) [1] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Large ) [price] => 15.80 ) ) ) ) [5] => SimpleXMLElement
Object ( [#attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Mediterranean ) [type] => Speciality [available] => true
[size] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => Small )
[price] => 9.80 ) [1] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => Large )
[price] => 15.80 ) ) ) ) )
What I want to do is to display this information in a sort of a list, so I'm wondering how can I access the elements of SimpleXMLElement Object.
Thanks
Cleaning up output:
[0] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => pizza )
[item] => SimpleXMLElement Object (
[#attributes] => Array ( [name] => Tomato and Cheese )
[type] => Regular
[available] => true
[size] => Array (
[0] => SimpleXMLElement Object (
[#attributes] => Array ([name] => Small )
[price] => 5.50 )
[1] => SimpleXMLElement Object (
[#attributes] => Array ([name] => Large )
[price] => 9.75 )
)
)
)
Something like this will work, though a lot of your data is wrapped up as attributes, which will make it a little more verbose to get to, like the sizes attribute. You may need to cast items in order to get the value.
$products = $this->_dom->xpath('//menu/category[#name="'.$category.'"]');
foreach ($products as $product) {
// accessing SimpleXML values is tricky
// $name is a SimpleXMLElement Object
$name = $product['title'];
// $name2 has the string value of the title attribute
$name2 = (string) $product['title'];
// following works because __toString is implemented
// by SimpleXMLElement class
echo "Title: " . $product['title'];
// alternative syntax, unless you cast to a
// string, you have a SimpleXML Object
$name3 = $product->attributes()->title;
}
See Access #attributes data in SimpleXMLElement in PHP

How can I separate these xml elements?

I got an xml response of
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.0
[msg_type] => response
)
[header] => SimpleXMLElement Object
(
[param] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => SN
[value] => 1012
)
)
...
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => ErrorInfo
[value] => NA
)
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => ProcessTime
[value] => 0
)
)
)
)
[Heartbeat] => SimpleXMLElement Object
(
)
)
I tried:
$version1 = (string) $xml->version; // expecting to get "2.0"
$errorinfo = (string) $xml->name->ErrorInfo; // expecting to get "NA"

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'];
}

Print element of array within array

This is my array structure. I want to print title and track id.
Array
(
[#attributes] => Array
(
[type] => array
)
[track] => SimpleXMLElement Object
(
[id] => 3617316
[created-at] => 2010-06-24T09:09:23Z
[user-id] => 1219873
[permalink] => kalimba
[duration] => 348213
[commentable] => true
[state] => finished
[sharing] => public
[tag-list] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[streamable] => true
[downloadable] => false
[genre] => SimpleXMLElement Object
(
)
[release] => SimpleXMLElement Object
(
)
[purchase-url] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[label-id] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[label-name] => SimpleXMLElement Object
(
)
[isrc] => SimpleXMLElement Object
(
)
[video-url] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[track-type] => demo
[key-signature] => SimpleXMLElement Object
(
)
[bpm] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[playback-count] => 7
[download-count] => 0
[comment-count] => 3
[favoritings-count] => 0
[title] => Kalimba
[release-year] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[release-month] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[release-day] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[original-format] => raw
[license] => all-rights-reserved
[uri] => http://api.soundcloud.com/tracks/3617316
[permalink-url] => http://soundcloud.com/rajanikant/kalimba
[artwork-url] => SimpleXMLElement Object
(
[#attributes] => Array
(
[nil] => true
)
)
[waveform-url] => http://waveforms.soundcloud.com/7eiQ56CHuzwB_m.png
[user] => SimpleXMLElement Object
(
[id] => 1219873
[permalink] => rajanikant
[username] => rajanikant
[uri] => http://api.soundcloud.com/users/1219873
[permalink-url] => http://soundcloud.com/rajanikant
[avatar-url] => http://i1.soundcloud.com/avatars-000001390005-a33xqd-large.jpg?77983a
)
[stream-url] => http://media.soundcloud.com/stream/7eiQ56CHuzwB
[download-url] => http://soundcloud.com/rajanikant/kalimba/download
[downloads-remaining] => 100
[user-playback-count] => 6
[user-favorite] => false
)
)
This way I am printing but it not show records.
<?php echo $me1[1][0];?>
If your objects is $xml
You want $xml->track->id
Reference:
http://ca2.php.net/manual/en/simplexml.examples-basic.php

Categories