php parse array in object detail - php

I got stuck on parsing array in my php object
stdClass Object
(
[result] => stdClass Object
(
[details] => Array
(
[0] => stdClass Object
(
[account] =>
[address] => add1
[category] => receive
[amount] => 1100
[label] =>
[vout] => 1
)
[1] => stdClass Object
(
[account] =>
[address] => add2
[category] => receive
[amount] => 11600
[label] =>
[vout] => 2
)
[2] => stdClass Object
(
[account] =>
[address] => add3
[category] => receive
[amount] => 1000
[label] =>
[vout] => 4
)
)
)
)
So how I can fetch all details indexes 0,1,2 etc

So, you could just iterate over details as below:
foreach($your_variable->result->details as $current_detail){
echo $current_detail->account;
// other code here
}

Your data is object if you converto to array u can simply do it
$array=json_decode(json_encode($data),true);
print_r($array);
If just one details key
$array=json_decode(json_encode($data),true)
print_r($array['result']['details']);

Related

Merging two objects based on key value

I have two objects from an API I am trying to merge, one is a list of available subscription packages and one is a list of the current user's active subscription's and the corresponding status.
The first object looks like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
)
)
[has_more] =>
)
This second object looks like this:
Array
(
[0] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
[1] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
I want to combine the second object with the first object if subscription->plan->product matches product->id so it would look something like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
)
)
[has_more] =>
)
I know I can perform two foreach loops on each object and turn them into arrays with the revenant data I need and combine them that way, but I would rather merge them complete as objects.
How can I merge/combine these two in PHP?
Edit: This was my attempt at merging the two, but I am lost on how to actually make it work:
foreach($array1 as $product) {
foreach($array2 as $subscription) {
if ($subscription->plan->product == $product->id){
//how do I append to the object in the right place (where the 0 is)?
$array1->data[0]->subscription = $subscription->plan;
}
}
}

how to retrieve variable from object

I want to retrieve $se_module value from my object. How can I get that? I can easily get others by $object->Owner->name. I've tried $object->{$se_module}, but got error as undefined variable.
stdClass Object
(
[Owner] => stdClass Object
(
[name] => Merv Henry
[id] => 2880896000003943001
)
[Modified_Time] => 2019-01-09T21:25:05-05:00
[$attachments] =>
[Created_Time] => 2019-01-09T21:25:05-05:00
[Parent_Id] => stdClass Object
(
[name] =>
[id] => 2880896000011553097
)
[$editable] => 1
[$se_module] => Deals
[Modified_By] => stdClass Object
(
[name] => Merv Henry
[id] => 2880896000003943001
)
[$size] =>
[$voice_note] =>
[id] => 2880896000011676081
[Created_By] => stdClass Object
(
[name] => Merv Henry
[id] => 2880896000003943001
)
[Note_Title] =>
[Note_Content] => This unit needs 1# DA97-12540K & 1# DA97-13718C
Quote is needed
)
you cane make it as array then you can get it
$data = (array)$se_module;
then can get data like this
$data['Modified_Time'];

Typeform api, change variables names in results

I am using typeform as a tool to get feedback and contact infos.
By using the api, I get results from my survey with the following format :
[answers] => Array
(
[0] => stdClass Object
(
[field] => stdClass Object
(
[id] => XdLvktpeAVmc
[type] => short_text
[ref] => 88e1657e-516b-4219-8086-52ba80e55eb0
)
[type] => text
[text] => Test1
)
[1] => stdClass Object
(
[field] => stdClass Object
(
[id] => QLYWXINAKoeS
[type] => number
[ref] => c431bf84-7305-4171-a659-67191b797651
)
[type] => number
[number] => 111111111
)
[2] => stdClass Object
(
[field] => stdClass Object
(
[id] => wRSL0qJ7BSWH
[type] => email
[ref] => 719312ce-9ae9-4a45-9b9f-fdfaf3d23dc1
)
[type] => email
[email] => Test1#email.com
)
)
[hidden] => stdClass Object
(
)
[calculated] => stdClass Object
(
[score] => 0
)
)
Is it possible to change the name of the variables (text, number, email) ? I've tried using hidden fields but it's not the feature to use.
Thanks

Softalyer API - calling getBlockDeviceTemplateGroups

we have a problem in using getBlockDeviceTemplateGroups(): It returns always "blockDevicesDiskSpaceTotal" values as zero.
PHP CODE:
$mask = "mask[name, blockDevicesDiskSpaceTotal, storageRepository[billingItem[associatedBillingItem]], createDate, datacenter, datacenters,id,parentId]";
$client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
$client->setObjectMask($mask);
//Retrieve private template group objects (parent and children) and the shared template group objects (parent only) for an account.
$templates = $client->getBlockDeviceTemplateGroups();
OUTPUT of print_r($templates):
Array
(
[0] => stdClass Object
(
[createDate] => 2016-02-29T17:42:04+01:00
[id] => 966271
[name] => 20160229-JMeter-Master.bluereply.it
[parentId] =>
[blockDevicesDiskSpaceTotal] => 0
[datacenters] => Array
(
[0] => stdClass Object
(
[id] => 814994
[longName] => Amsterdam 3
[name] => ams03
[statusId] => 2
)
[1] => stdClass Object
(
[id] => 815394
[longName] => Milan 1
[name] => mil01
[statusId] => 2
)
)
)
[1] => stdClass Object
(
[createDate] => 2016-02-29T17:31:10+01:00
[id] => 966247
[name] => 20160229-JMeter-Slave.bluereply.it
[parentId] =>
[blockDevicesDiskSpaceTotal] => 0
[datacenters] => Array
(
[0] => stdClass Object
(
[id] => 815394
[longName] => Milan 1
[name] => mil01
[statusId] => 2
)
[1] => stdClass Object
(
[id] => 814994
[longName] => Amsterdam 3
[name] => ams03
[statusId] => 2
)
[2] => stdClass Object
(
[id] => 449500
[longName] => Paris 1
[name] => par01
[statusId] => 2
)
)
and so on...
Thanks in advance
try using this objectMask:
mask[name, blockDevicesDiskSpaceTotal, storageRepository[billingItem[associatedBillingItem]],children[blockDevicesDiskSpaceTotal,datacenter], createDate, datacenter, datacenters,id,parentId]
the problem is that the images templates have a container which stores the real images templates for each datacenter, currently you are getting the "blockDevicesDiskSpaceTotal" of the container which is empty, you need to get that information form the container's children.
Regards

Freshbooks API show details

I am using the php library from here and I have a small problem.
To get some info for a invoice 118868, I can do this
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print_r($invoice);
?>
This is the output for print_r
Class Object ( [#attributes] => stdClass Object ( [status] => ok ) [invoice] => stdClass Object ( [invoice_id] => 00000219023 [estimate_id] => stdClass Object ( ) [number] => 8822 [client_id] => 83 [contacts] => stdClass Object ( [contact] => stdClass Object ( [contact_id] => 0 ) ) [recurring_id] => stdClass Object ( ) [organization] => Jimmy Thwart [first_name] => stdClass Object ( ) [last_name] => stdClass Object ( ) [p_street1] => stdClass Object ( ) [p_street2] => stdClass Object ( ) [p_city] => stdClass Object ( ) [p_state] => stdClass Object ( ) [p_country] => stdClass Object ( ) [p_code] => stdClass Object ( ) [po_number] => 10002 [status] => sent [amount] => 16.90 [amount_outstanding] => 16.90 [paid] => 0.00 [date] => 2013-01-31 00:00:00 [notes] => stdClass Object ( ) [terms] => Your slot can only be secured upon payment. Any reservation made before payment will only be guaranteed for 2 days. [discount] => 0 [url] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [auth_url] => https://example.freshbooks.com/invoices/219023 [links] => stdClass Object ( [client_view] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [view] => https://example.freshbooks.com/invoices/219023 [edit] => https://example.freshbooks.com/invoices/219023/edit ) [return_uri] => stdClass Object ( ) [updated] => 2013-01-31 02:25:13 [currency_code] => SGD [language] => en [vat_name] => stdClass Object ( ) [vat_number] => stdClass Object ( ) [folder] => active [staff_id] => 1 [lines] => stdClass Object ( [line] => stdClass Object ( [line_id] => 1 [name] => Lady 1pax [description] => Services (1 pax) [unit_cost] => 16.90 [quantity] => 1 [amount] => 16.90 [tax1_name] => stdClass Object ( ) [tax2_name] => stdClass Object ( ) [tax1_percent] => 0 [tax2_percent] => 0 [compound_tax] => 0 [type] => Item ) ) [gateways] => stdClass Object ( [gateway] => stdClass Object ( [name] => PayPal ) ) ) )
I hope the output can only be the URL and not this whole chunk of code.
Output wanted:
https://example.freshbooks.com/view/vgPb2TNGCR7n8JV
However, it lists all information of the invoice. How do I get only the invoice URL?
Well, using the output of your code, you can see what the elements of $invoice are. Use the one that you need.
Invoice is an instane of a class. You should treat it like all instances.
To get URL value you can do the next:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->url;
?>
or there's another possible value you're looking for:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->links->client_view;
?>
According to official API doc <url> is not supporting any more, so you should use <links> "element to provide your customers with direct links to the invoice for editing, viewing by the client and viewing by an administrator."

Categories