Retrieving attribute from query - php

How would I go about getting the balance from this result?
Braintree_Customer Object
(
[_attributes:protected] => Array
(
[id] => 425865
[merchantId] => abcddeff
[firstName] => Waka
[lastName] => Flaka
[company] => Waka Flaka Co.
[email] => demoemail#gmail.com
[phone] => 281.330.8004
[fax] => 419.555.1235
[website] => http://example.com
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[customFields] =>
[creditCards] => Array
(
[0] => Braintree_CreditCard Object
(
[_attributes] => Array
(
[bin] => 400551
[expirationMonth] => 09
[expirationYear] => 2020
[last4] => 0004
[billingAddress] => Braintree_Address Object
(
[_attributes] => Array
(
[id] => gx
[customerId] => 30007705
[firstName] => Flaka
[lastName] => Waka
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
[countryName] =>
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
)
)
[cardType] => Visa
[cardholderName] => Flaka Waka
[commercial] => Unknown
[countryOfIssuance] =>
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[customerId] => 30007705
[customerLocation] => US
[debit] => Yes
[default] => 1
[durbinRegulated] => Yes
[expired] =>
[healthcare] => Unknown
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox
[issuingBank] => Unknown
[payroll] => Unknown
[prepaid] => No
[subscriptions] => Array
(
[0] => Braintree_Subscription Object
(
[_attributes] => Array
(
[addOns] => Array
(
)
[balance] => 0.00
My code to receive the cardType is
$cardtype = $customer->creditCards[0]->cardType;

You can try this
$balance = $customer->creditCards[0]->subscriptions[0]->balance;

Related

Coinbase v2 response are too bad and cant get data from this Object class

I'm using coinbase and I'm trying to get data but the response is it to bad cant even generate to json or array and then to json...
Code:
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Resource\Address;
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);
$accounts = $client->getAccounts();
print_r($accounts); //Here is the response from down. But I dont know how to
get data from $accounts->data or anything else cause its not clear repsonse.
The Response is:
Coinbase\Wallet\Resource\ResourceCollection Object ( [previousUri:Coinbase\Wallet\Resource\ResourceCollection:private] => [nextUri:Coinbase\Wallet\Resource\ResourceCollection:private] => [resources:Coinbase\Wallet\Resource\ResourceCollection:private] => Array ( [0] => Coinbase\Wallet\Resource\Account Object ( [name:Coinbase\Wallet\Resource\Account:private] => LTC Wallet [primary:Coinbase\Wallet\Resource\Account:private] => [type:Coinbase\Wallet\Resource\Account:private] => wallet [currency:Coinbase\Wallet\Resource\Account:private] => LTC [balance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object ( [amount:Coinbase\Wallet\Value\Money:private] => 0 [currency:Coinbase\Wallet\Value\Money:private] => LTC ) [nativeBalance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object ( [amount:Coinbase\Wallet\Value\Money:private] => 0 [currency:Coinbase\Wallet\Value\Money:private] => EUR ) [createdAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object ( [date] => 2017-11-16 12:00:19.000000 [timezone_type] => 2 [timezone] => Z ) [updatedAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object ( [date] => 2018-01-17 19:10:56.000000 [timezone_type] => 2 [timezone] => Z ) [id:Coinbase\Wallet\Resource\Resource:private] => someID [resource:Coinbase\Wallet\Resource\Resource:private] => account [resourcePath:Coinbase\Wallet\Resource\Resource:private] => /v2/accounts/someID [rawData:Coinbase\Wallet\Resource\Resource:private] => Array ( [id] => someID [name] => LTC Wallet [primary] => [type] => wallet [currency] => LTC [balance] => Array ( [amount] => 1.00496384 [currency] => LTC ) [created_at] => 2017-11-16T12:00:19Z [updated_at] => 2018-01-17T19:10:56Z [resource] => account [resource_path] => /v2/accounts/someID [native_balance] => Array ( [amount] => 162.25 [currency] => EUR ) ) ) [1] => Coinbase\Wallet\Resource\Account Object ( [name:Coinbase\Wallet\Resource\Account:private] => ETH Wallet [primary:Coinbase\Wallet\Resource\Account:private] => [type:Coinbase\Wallet\Resource\Account:private] => wallet [currency:Coinbase\Wallet\Resource\Account:private] => ETH [balance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object ( [amount:Coinbase\Wallet\Value\Money:private] => 0 [currency:Coinbase\Wallet\Value\Money:private] => ETH ) [nativeBalance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object ( [amount:Coinbase\Wallet\Value\Money:private] => 0[currency:Coinbase\Wallet\Value\Money:private] => EUR ) [createdAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object ( [date] => 2017-11-16 12:00:19.000000 [timezone_type] => 2 [timezone] => Z ) [updatedAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object ( [date] => 2018-01-15 16:50:26.000000 [timezone_type] => 2 [timezone] => Z ) [id:Coinbase\Wallet\Resource\Resource:private] => someID [resource:Coinbase\Wallet\Resource\Resource:private] => account [resourcePath:Coinbase\Wallet\Resource\Resource:private] => /v2/accounts/someID [rawData:Coinbase\Wallet\Resource\Resource:private] => Array ( [id] => someID [name] => ETH Wallet [primary] => [type] => wallet [currency] => ETH [balance] => Array ( [amount] => 1.06493355 [currency] => ETH ) [created_at] => 2017-11-16T12:00:19Z [updated_at] => 2018-01-15T16:50:26Z [resource] => account [resource_path] => /v2/accounts/someID [native_balance] => Array ( [amount] => 942.46 [currency] => EUR ) ) ) [2] => Coinbase\Wallet\Resource\Account Object ( [name:Coinbase\Wallet\Resource\Account:private] => BTC Wallet [primary:Coinbase\Wallet\Resource\Account:private] => 1 [type:Coinbase\Wallet\Resource\Account:private] => wallet [currency:Coinbase\Wallet\Resource\Account:private] => BTC [balance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object ( [amount:Coinbase\Wallet\Value\Money:private] => 0 [currency:Coinbase\Wallet\Value\Money:private] => BTC ) [nativeBalance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object ( [amount:Coinbase\Wallet\Value\Money:private] => 0 [currency:Coinbase\Wallet\Value\Money:private] => EUR ) [createdAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object ( [date] => 2017-11-16 12:00:19.000000 [timezone_type] => 2 [timezone] => Z ) [updatedAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object ( [date] => 2018-01-19 21:32:51.000000 [timezone_type] => 2 [timezone] => Z ) [id:Coinbase\Wallet\Resource\Resource:private] => someID [resource:Coinbase\Wallet\Resource\Resource:private] => account [resourcePath:Coinbase\Wallet\Resource\Resource:private] => /v2/accounts/someID [rawData:Coinbase\Wallet\Resource\Resource:private] => Array ( [id] => someID [name] => BTC Wallet [primary] => 1 [type] => wallet [currency] => BTC [balance] => Array ( [amount] => 0.10962561 [currency] => BTC ) [created_at] => 2017-11-16T12:00:19Z [updated_at] => 2018-01-19T21:32:51Z [resource] => account [resource_path] => /v2/accounts/someID [native_balance] => Array ( [amount] => 1067.30 [currency] => EUR ) ) ) ) )
I knows its too big but I cant find a way to get each data.
$data->data
$data["data"]
$data[0]
None works..
You can use
$data = $client->decodeLastResponse();
print_r($data);
that will return an array with all the data you need.

How to access private scoped namespace array data in php?

Here is the array data:
Coinbase\Wallet\Resource\ResourceCollection Object
(
[previousUri:Coinbase\Wallet\Resource\ResourceCollection:private] =>
[nextUri:Coinbase\Wallet\Resource\ResourceCollection:private] => /v2/accounts/REMOVED/transactions?starting_after=REMOVED
[resources:Coinbase\Wallet\Resource\ResourceCollection:private] => Array
(
[0] => Coinbase\Wallet\Resource\Transaction Object
(
[type:Coinbase\Wallet\Resource\Transaction:private] => send
[status:Coinbase\Wallet\Resource\Transaction:private] => completed
[amount:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Value\Money Object
(
[amount:Coinbase\Wallet\Value\Money:private] => -0.00016600
[currency:Coinbase\Wallet\Value\Money:private] => BTC
)
[nativeAmount:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Value\Money Object
(
[amount:Coinbase\Wallet\Value\Money:private] => -0.07
[currency:Coinbase\Wallet\Value\Money:private] => USD
)
[description:Coinbase\Wallet\Resource\Transaction:private] => Your first bitcoin!
[fee:Coinbase\Wallet\Resource\Transaction:private] =>
[instantExchange:Coinbase\Wallet\Resource\Transaction:private] =>
[createdAt:Coinbase\Wallet\Resource\Transaction:private] => DateTime Object
(
[date] => 2016-05-18 21:04:04.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\Wallet\Resource\Transaction:private] => DateTime Object
(
[date] => 2016-05-18 21:16:50.000000
[timezone_type] => 2
[timezone] => Z
)
[network:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Value\Network Object
(
[status:Coinbase\Wallet\Value\Network:private] => confirmed
[hash:Coinbase\Wallet\Value\Network:private] => REMOVED
)
[to:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Resource\BitcoinAddress Object
(
[address:Coinbase\Wallet\Resource\BitcoinAddress:private] => 1BJcYDCMHJ3FAqj8YKbcnNq5xxHcxViD5s
[id:Coinbase\Wallet\Resource\Resource:private] =>
[resource:Coinbase\Wallet\Resource\Resource:private] => bitcoin_address
[resourcePath:Coinbase\Wallet\Resource\Resource:private] =>
[rawData:Coinbase\Wallet\Resource\Resource:private] =>
)
[from:Coinbase\Wallet\Resource\Transaction:private] =>
[address:Coinbase\Wallet\Resource\Transaction:private] =>
[application:Coinbase\Wallet\Resource\Transaction:private] =>
[buy:Coinbase\Wallet\Resource\Transaction:private] =>
[sell:Coinbase\Wallet\Resource\Transaction:private] =>
[id:Coinbase\Wallet\Resource\Resource:private] => REMOVED
[resource:Coinbase\Wallet\Resource\Resource:private] => transaction
[resourcePath:Coinbase\Wallet\Resource\Resource:private] => /v2/accounts/REMOVED/transactions/REMOVED
[rawData:Coinbase\Wallet\Resource\Resource:private] => Array
(
[id] => REMOVED
[type] => send
[status] => completed
[amount] => Array
(
[amount] => -0.00016600
[currency] => BTC
)
[native_amount] => Array
(
[amount] => -0.07
[currency] => USD
)
[description] => Your first bitcoin!
[created_at] => 2016-05-18T21:04:04Z
[updated_at] => 2016-05-18T21:16:50Z
[resource] => transaction
[resource_path] => /v2/accounts/REMOVED/transactions/REMOVED
[instant_exchange] =>
[network] => Array
(
[status] => confirmed
[hash] => REMOVED
)
[to] => Array
(
[resource] => bitcoin_address
[address] => 1BJcYDCMHJ3FAqj8YKbcnNq5xxHcxViD5s
)
[details] => Array
(
[title] => Sent bitcoin
[subtitle] => to Bitcoin address
)
)
)
[1] => Coinbase\Wallet\Resource\Transaction Object
(
[type:Coinbase\Wallet\Resource\Transaction:private] => send
[status:Coinbase\Wallet\Resource\Transaction:private] => completed
[amount:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Value\Money Object
(
[amount:Coinbase\Wallet\Value\Money:private] => -0.00016600
[currency:Coinbase\Wallet\Value\Money:private] => BTC
)
[nativeAmount:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Value\Money Object
(
[amount:Coinbase\Wallet\Value\Money:private] => -0.07
[currency:Coinbase\Wallet\Value\Money:private] => USD
)
[description:Coinbase\Wallet\Resource\Transaction:private] => Your first bitcoin!
[fee:Coinbase\Wallet\Resource\Transaction:private] =>
[instantExchange:Coinbase\Wallet\Resource\Transaction:private] =>
[createdAt:Coinbase\Wallet\Resource\Transaction:private] => DateTime Object
(
[date] => 2016-05-18 20:41:29.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\Wallet\Resource\Transaction:private] => DateTime Object
(
[date] => 2016-05-18 20:45:51.000000
[timezone_type] => 2
[timezone] => Z
)
[network:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Value\Network Object
(
[status:Coinbase\Wallet\Value\Network:private] => confirmed
[hash:Coinbase\Wallet\Value\Network:private] => REMOVED
)
[to:Coinbase\Wallet\Resource\Transaction:private] => Coinbase\Wallet\Resource\BitcoinAddress Object
(
[address:Coinbase\Wallet\Resource\BitcoinAddress:private] => 1BJcYDCMHJ3FAqj8YKbcnNq5xxHcxViD5s
[id:Coinbase\Wallet\Resource\Resource:private] =>
[resource:Coinbase\Wallet\Resource\Resource:private] => bitcoin_address
[resourcePath:Coinbase\Wallet\Resource\Resource:private] =>
[rawData:Coinbase\Wallet\Resource\Resource:private] =>
)
[from:Coinbase\Wallet\Resource\Transaction:private] =>
[address:Coinbase\Wallet\Resource\Transaction:private] =>
[application:Coinbase\Wallet\Resource\Transaction:private] =>
[buy:Coinbase\Wallet\Resource\Transaction:private] =>
[sell:Coinbase\Wallet\Resource\Transaction:private] =>
[id:Coinbase\Wallet\Resource\Resource:private] => REMOVED
[resource:Coinbase\Wallet\Resource\Resource:private] => transaction
[resourcePath:Coinbase\Wallet\Resource\Resource:private] => /v2/accounts/REMOVED/transactions/REMOVED
[rawData:Coinbase\Wallet\Resource\Resource:private] => Array
(
[id] => REMOVED
[type] => send
[status] => completed
[amount] => Array
(
[amount] => -0.00016600
[currency] => BTC
)
[native_amount] => Array
(
[amount] => -0.07
[currency] => USD
)
[description] => Your first bitcoin!
[created_at] => 2016-05-18T20:41:29Z
[updated_at] => 2016-05-18T20:45:51Z
[resource] => transaction
[resource_path] => /v2/accounts/REMOVED/transactions/REMOVED
[instant_exchange] =>
[network] => Array
(
[status] => confirmed
[hash] => REMOVED
)
[to] => Array
(
[resource] => bitcoin_address
[address] => 1BJcYDCMHJ3FAqj8YKbcnNq5xxHcxViD5s
)
[details] => Array
(
[title] => Sent bitcoin
[subtitle] => to Bitcoin address
)
)
)
I am trying to figure out how to access a few of these variables.
For instance, how can I make this happen:
$bitcoin_address = [address:Coinbase\Wallet\Resource\Address:private];
$type = [type:Coinbase\Wallet\Resource\Transaction:private];
I know you can't access private scopes, but is there any way around this? Can I scrape the data if there isn't a better way?
Thanks.
Yeah, the best way to access to your data is using decodeLastResponse Function.
Its allow to pull out all the data you need in those arrays.
$address = $client->getAccountAddress($account, $addressId);
$transactions = $client->getAddressTransactions($address);
$transactions = $client->decodeLastResponse();
echo var_dump($transactions['data']);
Almost all this private properties has public getters (public methods that allow you to access the private properties).
$bitcoinAddresses = [];
$types = [];
foreach ($resourceCollection->all() as $transaction) {
$bitcoinAddresses[] = $transaction->getTo()->getAddress();
$types[] = $transaction->getType();
}
If you visit coinbase/coinbase-php repo, you can see the source code there. There are all the classes you described with all the attributes and methods.
You need to call function decodeLastResponse like this:
$addresses = $client->getAccountAddresses($account)
$addresses = $client->decodeLastResponse();

access attribute array from transaction object

from the response of braintree. access attribute array from transaction object
which way to access it?
Braintree\Transaction Object (
[_attributes:protected] => Array (
[id] => 3ytqkd
[status] => authorized
[type] => sale
[currencyIsoCode] => USD
[amount] => 100.00
[merchantAccountId] => arvaan
[orderId] =>
[createdAt] => DateTime Object
(
[date] => 2015-12-01 07:15:40
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-12-01 07:15:40
[timezone_type] => 3
[timezone] => UTC
)
[customer] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[email] =>
[website] =>
[phone] =>
[fax] =>
)
[billing] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
[refundId] =>
[refundIds] => Array
(
)
[refundedTransactionId] =>
[partialSettlementTransactionIds] => Array
(
)
[authorizedTransactionId] =>
[settlementBatchId] =>
[shipping] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
[customFields] =>
[avsErrorResponseCode] =>
[avsPostalCodeResponseCode] => I
[avsStreetAddressResponseCode] => I
[cvvResponseCode] => M
[gatewayRejectionReason] =>
[processorAuthorizationCode] => DHYKZ3
[processorResponseCode] => 1000
[processorResponseText] => Approved
[additionalProcessorResponse] =>
[voiceReferralNumber] =>
[purchaseOrderNumber] =>
[taxAmount] =>
[taxExempt] =>
[creditCard] => Array
(
[token] =>
[bin] => 555555
[last4] => 4444
[cardType] => MasterCard
[expirationMonth] => 12
[expirationYear] => 2017
[customerLocation] => US
[cardholderName] =>
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/mastercard.png?environment=sandbox
[prepaid] => Unknown
[healthcare] => Unknown
[debit] => Unknown
[durbinRegulated] => Unknown
[commercial] => Unknown
[payroll] => Unknown
[issuingBank] => Unknown
[countryOfIssuance] => Unknown
[productId] => Unknown
[uniqueNumberIdentifier] =>
[venmoSdk] =>
)
[statusHistory] => Array
(
[0] => Braintree\Transaction\StatusDetails Object
(
[_attributes:protected] => Array
(
[timestamp] => DateTime Object
(
[date] => 2015-12-01 07:15:40
[timezone_type] => 3
[timezone] => UTC
)
[status] => authorized
[amount] => 100.00
[user] => jitendra.arvaan
[transactionSource] => api
)
)
)
[planId] =>
[subscriptionId] =>
[subscription] => Array
(
[billingPeriodEndDate] =>
[billingPeriodStartDate] =>
)
[addOns] => Array
(
)
[discounts] => Array
(
)
[descriptor] => Braintree\Descriptor Object
(
[_attributes:protected] => Array
(
[name] =>
[phone] =>
[url] =>
)
)
[recurring] =>
[channel] =>
[serviceFeeAmount] =>
[escrowStatus] =>
[disbursementDetails] => Braintree\DisbursementDetails Object
(
[_attributes:protected] => Array
(
[disbursementDate] =>
[settlementAmount] =>
[settlementCurrencyIsoCode] =>
[settlementCurrencyExchangeRate] =>
[fundsHeld] =>
[success] =>
)
)
[disputes] => Array
(
)
[paymentInstrumentType] => credit_card
[processorSettlementResponseCode] =>
[processorSettlementResponseText] =>
[threeDSecureInfo] =>
[creditCardDetails] => Braintree\Transaction\CreditCardDetails Object
(
[_attributes:protected] => Array
(
[token] =>
[bin] => 555555
[last4] => 4444
[cardType] => MasterCard
[expirationMonth] => 12
[expirationYear] => 2017
[customerLocation] => US
[cardholderName] =>
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/mastercard.png?environment=sandbox
[prepaid] => Unknown
[healthcare] => Unknown
[debit] => Unknown
[durbinRegulated] => Unknown
[commercial] => Unknown
[payroll] => Unknown
[issuingBank] => Unknown
[countryOfIssuance] => Unknown
[productId] => Unknown
[uniqueNumberIdentifier] =>
[venmoSdk] =>
[expirationDate] => 12/2017
[maskedNumber] => 555555******4444
)
)
[customerDetails] => Braintree\Transaction\CustomerDetails Object
(
[_attributes:protected] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[email] =>
[website] =>
[phone] =>
[fax] =>
)
)
[billingDetails] => Braintree\Transaction\AddressDetails Object
(
[_attributes:protected] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
)
[shippingDetails] => Braintree\Transaction\AddressDetails Object
(
[_attributes:protected] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
)
[subscriptionDetails] => Braintree\Transaction\SubscriptionDetails Object
(
[_attributes:protected] => Array
(
[billingPeriodEndDate] =>
[billingPeriodStartDate] =>
)
)
)
)
can access object using magic method
$transaction_id = $result->transaction->__get('id');
you havr to loop through the response you have got using foreach(),
foreach($reas->_attributes as $key => $val) {
echo $key;// will give you the attribute name
is_array($val) ? print_r($val) : echo $val;// will give you the attribute value
}

Get Value from an array with foreach

i've an format array like this
Array (
[0] => stdClass Object ( [subscriptionContractId] => 20878 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-02 03:27:39.000 [endDate] => 2015-07-04 03:27:39.000 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 2 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => 2015-07-03 03:27:39.000 [lastPaymentDate] => 2015-07-02 03:27:39.000 [subscriptionContractHistory] =>Array ( [0] => stdClass Object ( [subscriptionContractId] => 20878 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-07-01 03:27:53.290 ) ) [subscriptionContractLogs] => )
[1] => stdClass Object ( [subscriptionContractId] => 20861 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:31:47.000 [endDate] => 2015-06-30 11:32:05.150 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:31:47.000 [subscriptionContractHistory] => Array ( [0] =>
stdClass Object ( [subscriptionContractId] => 20861 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:31:55.530 ) [1] => stdClass Object ( [subscriptionContractId] => 20861 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:32:05.977 ) ) [subscriptionContractLogs] => )
[2] => stdClass Object ( [subscriptionContractId] => 20860 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:29:37.000 [endDate] => 2015-06-30 11:30:19.887 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:29:37.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:10.267 ) [1] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:20.687 ) ) [subscriptionContractLogs] => )
[3] => stdClass Object ( [subscriptionContractId] => 20859 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:27:33.000 [endDate] => 2015-06-30 11:27:57.683 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:27:33.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:42.173 ) [1] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:58.467 ) ) [subscriptionContractLogs] => )
i want to take this value [subscriptionContractId] => 20859
PFB my code
foreach($getContract as $key =>$value) {
$cancel = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
with that above code only showing single value of record, meanwhile record is around 27 records.
and another code, i try to rectify with initialize array, PFB.
$cancel = array()
foreach($getContract as $key =>$value) {
$cancel[] = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
i need only the value, of the array such as 20879, etc. eventhough i use double loop to extract particular array it only show the last record on 10663.
Please help
You can modify your code as follows (see php documenation about foreach for more background):
$cancel = array();
foreach($getContract as $value) {
$cancel[] = $value->subscriptionContractId;
...
}
This way, in every iteration $value is assigned one value from your getContract array, with it's key just being omitted. Then you can store the value's subscriptionContractId in your $cancel array to do whatever you are planning to do.
Note: briosheje is right about the line:
$this->data['res'] = $cancel;
$this->data['res'] is overwritten in every iteration. As you are overwriting it with the your $cancel array, your code should still work, but your doing unnecessary work here, as your array might be subjected to further change in the next iteration.
My advice would be to move this line after your loop brackets ended, so that your result is saved just once:
...
foreach($getContract as $value) {
...
}
$this->data['res'] = $cancel;

Remove empty array in multidimensional array using laravel phpexcel

I want remove array if element having empty or count '0'.I used PHPEXCEL in laravel.
My Code:
$results=Excel::load('assets/uploads/studentdata/file.xlsx', function($reader) {
})->get()->toArray();
I got output is following like,
Array ( [0] => Array ( [grade] => 12 [schoolentrancedate] => Carbon\Carbon Object ( [date] => 2014-09-02 00:00:00
[timezone_type] => 3 [timezone] => UTC ) [schoolexitdate] => Carbon\Carbon Object ( [date] => 2015-06-25 00:00:00
[timezone_type] => 3 [timezone] => UTC ) [studentfirstname] => Gutierrez [studentmiddlename] => [studentlastname] => Larry
[age] => [gender] => M [dateofbirth] => Carbon\Carbon Object ( [date] => 1997-06-16 00:00:00 [timezone_type] => 3 [timezone]
=> UTC ) [language] => [parentfullname] => Irma Perlera [phone] => 617-778-3913 [mobile] => 617-778-3913 [parentemail] =>
info1234#gmail.com [emergencypersonfullname] => Irma Perlera [emergencypersonphone] => 617-778-3913 [emergencypersonmobile] =>
617-778-3913 [house] => 237 [apartment] => [street] => Ferry [city] => Everett [state] => MA [zipcode] => [weekdayfrom] =>
[weekdayto] => [intime] => [outtime] => [mondayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type]
=> 3 [timezone] => UTC ) [mondayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3
[timezone] => UTC ) [tuesdayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] =>
UTC ) [tuesdayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3 [timezone] => UTC )
[wednesdayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] => UTC )
[wednesdayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 13:30:00 [timezone_type] => 3 [timezone] => UTC )
[thursdayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] => UTC )
[thursdayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3 [timezone] => UTC )
[fridayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] => UTC )
[fridayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3 [timezone] => UTC ) [note] =>
[oneway] => [fromhome] => [fromschool] => [twoway] => [43] => )
[1] => Array ( [grade] => [schoolentrancedate] => [schoolexitdate] => [studentfirstname] => [studentmiddlename] =>
[studentlastname] => [age] => [gender] => [dateofbirth] => [language] => [parentfullname] => [phone] => [mobile] =>
[parentemail] => [emergencypersonfullname] => [emergencypersonphone] => [emergencypersonmobile] => [house] => [apartment] =>
[street] => [city] => [state] => [zipcode] => [weekdayfrom] => [weekdayto] => [intime] => [outtime] => [mondayintime] =>
[mondayouttime] => [tuesdayintime] => [tuesdayouttime] => [wednesdayintime] => [wednesdayouttime] => [thursdayintime] =>
[thursdayouttime] => [fridayintime] => [fridayouttime] => [note] => [oneway] => [fromhome] => [fromschool] => [twoway] => [43]
=> )
[2] => Array ( [grade] => [schoolentrancedate] => [schoolexitdate] => [studentfirstname] => [studentmiddlename] =>
[studentlastname] => [age] => [gender] => [dateofbirth] => [language] => [parentfullname] => [phone] => [mobile] =>
[parentemail] => [emergencypersonfullname] => [emergencypersonphone] => [emergencypersonmobile] => [house] => [apartment] =>
[street] => [city] => [state] => [zipcode] => [weekdayfrom] => [weekdayto] => [intime] => [outtime] => [mondayintime] =>
[mondayouttime] => [tuesdayintime] => [tuesdayouttime] => [wednesdayintime] => [wednesdayouttime] => [thursdayintime] =>
[thursdayouttime] => [fridayintime] => [fridayouttime] => [note] => [oneway] => [fromhome] => [fromschool] => [twoway] => [43]
=> ) )
I need to remove above array if array[1] ,array[2].. is empty or count '0' means its should be remove or if any other solution to avoid to read empty rows using phpexcel in laravel.
Try this..
$data="your array";
for($i=0;$i<count($data);$i++)
{
$countstring=count(array_filter($data[$i]));
if($countstring==0)
{
unset($data[$i]); // remove empty array
} else {
$finaldata[] =$data[$i]; //get non empty array
}
}
print_r($finaldata);

Categories