How to refer to Class Objects in PHP - php

I need to refer to each of components like to,message-price... etc
stdClass Object (
[message-count] => 1
[messages] => Array (
[0] => stdClass Object (
[to] => 256754106703
[message-price] => 0.00700000
[status] => 0
[message-id] => 020000001AF86AE5
[remaining-balance] => 1.80010000
[network] => 64101
)
)
)
How do I do it ?

do:
echo $yourObj->messages[0]->{'message-price'};

Related

get #attributes out of array from simplexml_load_file after xpath with php

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).

how to read values from an assosiative array using php?

This is my sample associative array:
Array (
[0] => Array (
[Name] => ALCOIN
[BasePlugin] => HTTP
[Version] => 1
[Description] => Plugin for ALCO_IN operations
[ImagePath] => ./resources
[xip] => http://www.example.org/XIP
[xsi] => http://www.w3.org/2001/XMLSchema-instance
[schemaLocation] => http://www.example.org/XIP XIP.xsd
)
[1] => Array (
[xip:Action] => Array (
[#attributes] => Array (
[Name] => OfferActivationByOfferID
[Version] => 1.0
[ImagePath] => ./resources
)
)
)
)
In that array I need to get the BasePlugin value and the name attribute value.
It seems you might need this:
$basePlugin = $yourArray[0]['BasePlugin'];
$attributes = $yourArray[1]['xip:Action']['#attributes'];
Assuming you are assigned to $yourVar like:
$yourVar = Array (
[0] => Array (
[Name] => ALCOIN
[BasePlugin] => HTTP
[Version] => 1
[Description] => Plugin for ALCO_IN operations
[ImagePath] => ./resources
[xip] => http://www.example.org/XIP
[xsi] => http://www.w3.org/2001/XMLSchema-instance
[schemaLocation] => http://www.example.org/XIP XIP.xsd
)
[1] => Array (
[xip:Action] => Array (
[#attributes] => Array (
[Name] => OfferActivationByOfferID
[Version] => 1.0
[ImagePath] => ./resources
)
)
)
)
You would use:
echo $yourVar[0]["BasePlugin"];
echo $yourVar[1]["xip:Action"]["#attributes"]["Name"];
echo $yourVar[1]["xip:Action"]["#attributes"]["Version"];
echo $yourVar[1]["xip:Action"]["#attributes"]["ImagePath"];

Push array object into another array object in php

I'm getting an error while pushing one object into another object. But the 2nd object is an array and inside an array there is an object. How can I fix this cause I want to add that into my object
My object just like this
I want to add the the Object2 into Object1
Objet1
stdClass Object
(
[id_laporan_pemeriksa] => 5
[no_pkpt] => SNE
[tgl_pkpt] => 2010
[no_penugasan] => ST-4000/PW25/2/2017
[tgl_penugasan] => 2017-08-09
[judul_laporan] => Masukkan Kode disini
[no_laporan] => LBINA-9000/PW25/2/2017
[tgl_laporan] => 2017-08-01
[tahun_anggaran_penugasan] => 2009
[nilai_anggaran_penugasan] => 10000000
[realisasi_anggaran_penugasan] => 100000000
[jenis_anggaran_penugasan] => Utang
[sumber_laporan] => Inspektorat Maluku
[nama_sumber_penugasan] => PKPT
[nama_ketua_tim] => Abdul Rofiek, Ak.
[nama_pengendali_teknis] => Alfian Massagony, S.E.
[nama_unit_penugasan] => Irban Wil. I
[nama_penugasan] => Penjaminan
[nama_sub_penugasan] => Audit
[id_s_sub_penugasan] => 010105
[nama_s_sub_penugasan] => Audit atas hal-hal lain di bidang kepegawaian.
)
Object2
stdClass Object
(
[id] => 3
[data_sebab] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_rekomendasi] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 9
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
[1] => stdClass Object
(
[id] => 5
[id_rekomendasi] =>
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 10
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 11
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
)
)
)
)
I have tried
$Object1['data']->$Object2;
But i got an error
Cannot use object of type stdClass as array
The syntax of adding $Object2 as a property of $Object1 is:
$Object1->Object2 = $Object2;
Or:
$Object1->{'Object2'} = $Object2;
It should be:
$Object1->data = $Object2; // it will create data element with obj2 as value
As the objects are objects and not arrays, using:
$Object1['data']->$Object2;
wont work. However doing the following will work:
$Object1->data = $Object2;

How to get value out of this...?

Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);

HubSpot api json decode

I am trying to parse some data out of the Hubspot API response. The response looks like this json_decoded:
stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
(
)
[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
(
[lastname] => stdClass Object
(
[value] => testtt
)
[firstname] => stdClass Object
(
[value] => test
)
[lastmodifieddate] => stdClass Object
(
[value] => 1411052906670
)
)
[form-submissions] => Array
(
[0] => stdClass Object
(
[conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
[timestamp] => 1411052834097
[form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
[portal-id] => 401824
[page-url] => http://wbl-1.hs-sites.com/test
[canonical-url] => http://wbl-1.hs-sites.com/test
[content-type] => landing-page
[page-title] => test
[page-id] => 1570433242
[title] => Default Form (Sample)
[first-visit-url] => http://wbl-1.hs-sites.com/test
[first-visit-timestamp] => 1411052722970
[meta-data] => Array
(
)
)
)
[list-memberships] => Array
(
)
[identity-profiles] => Array
(
[0] => stdClass Object
(
[vid] => 24
[identities] => Array
(
[0] => stdClass Object
(
[type] => EMAIL
[value] => test#user.com
[timestamp] => 1411052834097
)
[1] => stdClass Object
(
[type] => LEAD_GUID
[value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
[timestamp] => 1411052834201
)
)
)
)
[merge-audits] => Array
(
)
)
I'm looking specifically to try to dig out an email inside the indentities-profile area.
I've tried to do the following:
echo $results->contacts[0]->identity-profiles;
But it just gives me a value of 0
Then I try to go further into the array by doing:
echo $results->contacts[0]->identity-profiles[0];
But at that point - I get a parse error:
Parse error: syntax error, unexpected '['
What am I doing wrong? And how can I dig all the way down to
identity-profiles[0]->identities->[0]->value
which should equal: test#user.com
What am I missing?
As mentioned in the comment I would suggest to decode the JSON to an associative array by passing true as second parameter to json_decode. Example: json_decode($data, true) Than you could access your identity profiles by:
$results['contacts'][0]['identitiy-profiles']
If you still want to get the results as an object you have to access the properties the following way because they contain a -:
$results->contacts[0]->{'identity-profiles'}

Categories