The following output is a result of calling var_export($charge);
How can I access the output of 'paid'=>true from $charge? Any ideas would be appreciated.
I have tried $charge->_values->paid, $charge->paid, etc.. I haven't had any luck.
I have also tried $charge['_values']['paid'];
Stripe_Charge::__set_state(array(
'_apiKey' => 'sk_test_BPZyFpcAM',
'_values' =>
array (
'id' => 'ch_102kMF29T6',
'object' => 'charge',
'created' => 1381688,
'livemode' => false,
'paid' => true,
'amount' => 104000,
'currency' => 'usd',
'refunded' => false,
'card' =>
Stripe_Card::__set_state(array(
'_apiKey' => 'sk_test_BPZyFpc',
'_values' =>
array (
'id' => 'card_102kMF29T6',
'object' => 'card',
'last4' => '4242',
'type' => 'Visa',
'exp_month' => 2,
'exp_year' => 2015,
'fingerprint' => '7sRY4jiFM',
'customer' => NULL,
'country' => 'US',
'name' => NULL,
'address_line1' => NULL,
'address_line2' => NULL,
'address_city' => NULL,
'address_state' => NULL,
'address_zip' => NULL,
'address_country' => NULL,
'cvc_check' => 'pass',
'address_line1_check' => NULL,
'address_zip_check' => NULL,
),
'_unsavedValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_transientValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_retrieveOptions' =>
array (
),
)),
'captured' => true,
'refunds' =>
array (
),
'balance_transaction' => 'txn_102kMF29T6Z',
'failure_message' => NULL,
'failure_code' => NULL,
'amount_refunded' => 0,
'customer' => NULL,
'invoice' => NULL,
'description' => 'admin#fra.org',
'dispute' => NULL,
'metadata' =>
array (
),
),
'_unsavedValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_transientValues' =>
Stripe_Util_Set::__set_state(array(
'_elts' =>
array (
),
)),
'_retrieveOptions' =>
array (
),
))
You can use the __toArray($recursive = false) function to get the data in array form.
Example:
$chargeArray = $charge->__toArray(true);
echo $chargeArray['paid'];
I was also able to access the object's data using an array structure before even converting to an array. For me, $charge['paid'] gets me the value.
you can use __toArray() method:
$array = $collection->__toArray(true);
This will work for this case
whatever returns please parse it with json_decode and you will get a assoc array in php
i.e suppose return array is $ret
$ret = json_decode($ret);
$ret->paid;
$ret->captured;
$ret->card->id;
and so on..
Hope it will help you.
echo gettype($charge->paid); //boolean
var_dump($charge->paid); //bool(true)
if($charge->paid==true)echo "==true"; //==true
if($charge->paid===true)echo "===true"; //===true
if($charge->paid==1)echo "==1"; //==1
if($charge->paid===1)echo "===1"; //nada, it's a boolean, not numeric
So using if($charge->paid===true), you should be guaranteed that you are testing what you want to know.
Here in the output via print_r, you can see that paid is displayed as 1.
[_values:protected] => Array ( [id] => ch_10as29724hknftGBm9TxC [object] => charge [created] => 1384696845 [livemode] => [paid] => 1 [amount] => 1400
Thus, depending on where and how paid is being evaluated, it may appear as a number, string, or a boolean. You could run a series of tests for the three possibilities, or just use if($charge->paid){...}
When paid is false, it would probably be returned as empty (""), 0, or a boolean false. A possible thing to look out for is the boolean false being transposed to a string, in which case it would be interpreted as true.
$val = "false";
if($val){
echo "true"; //true
}
So, in your test, just add intval($val) to normalize the values and be on the safe side.
if(intval($charge->paid)){...}
Cheers
Related
This is my array.
array (
0 =>
(object) array(
'per_unit_price' => NULL,
'code' => 'dynamic_labour_cost',
'text_value' => '1000',
),
1 =>
(object) array(
'per_unit_price' => NULL,
'code' => 'dynamic_metal_weight',
'text_value' => '10',
),
2 =>
(object) array(
'per_unit_price' => NULL,
'code' => 'dynamic_stone_carat',
'text_value' => '10',
),
3 =>
(object) array(
'per_unit_price' => NULL,
'code' => 'dynamic_stone2_carat',
'text_value' => '10',
),
4 =>
(object) array(
'per_unit_price' => '10.00',
'code' => 'dynamic_metal',
'text_value' => NULL,
),
5 =>
(object) array(
'per_unit_price' => '20.00',
'code' => 'dynamic_stone',
'text_value' => NULL,
),
6 =>
(object) array(
'per_unit_price' => '50.00',
'code' => 'dynamic_stone2',
'text_value' => NULL,
),
)
In short,
Let's take an example =
I want to multiply => array[1]['text_value'] * array[4]['per_unit_price']
Because
array[1]['code'] = dynamic_metal_weight is substring of array[4]['code'] = dynamic_metal
As all code are dynamic so I cannot hard code condition.
And I tried using for each [scan all the array] but It will take a lot of time by traditional loops.
Please guide me.
As suggested by #Chris Haas, this solution is based on the same logic. Based on what I see above in problem description below could help you. Also have provided suggestions to improvise..
<?php
echo "<pre>";
// array of data received from database
$code_values = [
[
'per_unit_price' => NULL,
'code' => 'dynamic_labour_cost',
'text_value' => '1000'
],
[
'per_unit_price' => NULL,
'code' => 'dynamic_metal_weight',
'text_value' => '10'
],
[
'per_unit_price' => NULL,
'code' => 'dynamic_stone_carat',
'text_value' => '10'
],
[
'per_unit_price' => NULL,
'code' => 'dynamic_stone2_carat',
'text_value' => '10'
],
[
'per_unit_price' => '10.00',
'code' => 'dynamic_metal',
'text_value' => NULL
],
[
'per_unit_price' => '20.00',
'code' => 'dynamic_stone',
'text_value' => NULL
],
[
'per_unit_price' => '50.00',
'code' => 'dynamic_stone2',
'text_value' => NULL
]
];
// filter $code_values to extract an array of elements where "per_unit_price" is set i.e. text_value is NULL
// Suggestion to improvise: If from database, you can fetch codes where only per_unit_price are set then this array_filter will not be required
$per_unit_prices = array_filter($code_values, function($array_element) {
return is_null($array_element['text_value']);
});
// create array of per unit prices where code will be key and per_unit_price will be value
$per_unit_prices = array_column($per_unit_prices, 'per_unit_price', 'code');
echo "<br>per_unit_prices<br>";
print_r($per_unit_prices);
// filter $code_values to extract an array of elements where "text_value is set i.e. per_unit_price is NULL
// Suggestion to improvise: If you can fetch codes where only text_values are set then this array_filter will not be required
$text_values = array_filter($code_values, function($array_element) {
return is_null($array_element['per_unit_price']);
});
// create array of text_values where code will be key and text_value will be value
$text_values = array_column($text_values, 'text_value', 'code');
echo "<br>text_values<br>";
print_r($text_values);
$output = []; // array to store multiplication of per_unit_price and text_value of matching elements
foreach($text_values as $code => $value) {
// create key of per_unit_price by removing last string part starting with _
$per_unit_prices_key = implode('_', array_slice(explode('_', $code), 0, -1));
// check if per_unit_prices_key exits in $per_unit_prices array we created. If so, create output value
if(isset($per_unit_prices[$per_unit_prices_key])) {
$output[$code] = (float) $value * (float)$per_unit_prices[$per_unit_prices_key];
}
}
echo "<br>output<br>";
print_r($output);
This is the output of the above code.
per_unit_prices
Array
(
[dynamic_metal] => 10.00
[dynamic_stone] => 20.00
[dynamic_stone2] => 50.00
)
text_values
Array
(
[dynamic_labour_cost] => 1000
[dynamic_metal_weight] => 10
[dynamic_stone_carat] => 10
[dynamic_stone2_carat] => 10
)
output
Array
(
[dynamic_metal_weight] => 100
[dynamic_stone_carat] => 200
[dynamic_stone2_carat] => 500
)
I am a complete beginner in PHP. However I know how to output the value of custom field. I am having a bit of problems with arrays. The post meta key is fw_options. The value has multiple arrays and looks like this:
array (
0 =>
array (
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array (
'time' => 'fixed',
'hourly' =>
array (
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array (
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>
array (
),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array (
0 => '717',
),
),
)
I have this code which I have tried with no success:
$array = get_post_meta( get_the_ID(), 'fw_options', true );
echo $array[0]['reading_type']['fixed']['reading_times'];
How can I output the value 500 from the post meta key reading_times?
Simple Print array according to key
First hold the value into a variable than print according to array key
$data = array(
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array(
'time' => 'fixed',
'hourly' =>
array(
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array(
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>array(),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array(
0 => '717',
),
);
echo $data['reading_type']['fixed']['reading_times'];
Output #500
array (
'_attributes' =>
array (
'name' => 'Rothco Product API - Variations',
),
'item_variations' =>
array (
0 =>
stdclass::__set_state(array(
'item_variation_id' => 2,
'item_index' => 3146,
'rothco_item_no' => '10002',
'upc' => '023063601212',
'inventory' => 99,
'created_date' => '2014-11-28 10:06:45.000',
'weight' => '.4000',
'image_filename' => '10002-A.jpg',
'catalog_page_no' => 183,
'msrp' => '20.9900',
'map' => '.0000',
'diameter' => '',
'price' => '8.100000',
'case_price' => NULL,
'case_quantity' => NULL,
'statuses' => '',
)),
),
)
This is my array $list.
I want to access 'item_variations' value from this array,
but if I try $list['item_variations'], or $list->['item_variations']
it is not working
If you want to reach just item_variations then
echo $list['item_variations'];
is sufficient. Things get more tricky if you would like to i.e. get value if created_date from your sample data as you got mix of arrays and objects so that require different access:
echo $list['item_variations'][0]->created_date;
which would output
2014-11-28 10:06:45.000
I am getting weird problem in google php calendar api. It is not returning nextsynctoken.
This is var_export of $service->events->listEvents($calendarId, $optParams);
Google_Service_Calendar_Events::__set_state(array(
'collection_key' => 'items',
'accessRole' => 'owner',
'defaultRemindersType' => 'Google_Service_Calendar_EventReminder',
'defaultRemindersDataType' => 'array',
'description' => NULL,
'etag' => '"p32o8lfd2qbkt20g"',
'itemsType' => 'Google_Service_Calendar_Event',
'itemsDataType' => 'array',
'kind' => 'calendar#events',
'nextPageToken' => NULL,
'nextSyncToken' => NULL,
'summary' => 'Meetings',
'timeZone' => '',
'updated' => '2017-01-30T10:18:50.782Z',
'internal_gapi_mappings' =>
array (
),
'modelData' =>
array (
'defaultReminders' =>
array (
),
'items' =>
array (
),
),
'processed' =>
array (
),
))
While in api explorer, it is sending nextsynctoken.
Github link:
https://github.com/google/google-api-php-client/issues/1141
Thanks in advance.
Thanks guys I got answer. Her
https://github.com/google/google-api-dotnet-client/issues/610
How do I get the value of 'name'?
I've been trying various methods, including array($charge) and __toArray.
First works, second does not:
$charge_id = $charge->id;
echo $charge_id;
$name=$charge->_values->name;
echo $name;
Here is portion of the object returned by Stripe (up to the name I want to extract):
Stripe\Charge::__set_state(array(
'_opts' =>
Stripe\Util\RequestOptions::__set_state(array(
'headers' =>
array (
),
'apiKey' => 'sk_test_...',
)),
'_values' =>
array (
'id' => 'ch_73JaKii01nmbpZ',
'object' => 'charge',
'created' => 1443251926,
'livemode' => false,
'paid' => true,
'status' => 'succeeded',
'amount' => 1000,
'currency' => 'usd',
'refunded' => false,
'source' =>
Stripe\Card::__set_state(array(
'_opts' =>
Stripe\Util\RequestOptions::__set_state(array(
'headers' =>
array (
),
'apiKey' => 'sk_test_...',
)),
'_values' =>
array (
'id' => 'card_73JZrJCDzl877J',
'object' => 'card',
'last4' => '1111',
'brand' => 'Visa',
'funding' => 'unknown',
'exp_month' => 12,
'exp_year' => 2021,
'fingerprint' => 'hhzQQZIXaAVDNMPP',
'country' => 'US',
'name' => 'Steve Veltkamp',
according to this:
https://github.com/stripe/stripe-php/blob/master/lib/Charge.php
you may try to play with retrieve method:
$charge::retrieve($charge->id);
or ( https://github.com/stripe/stripe-php/blob/master/lib/StripeObject.php ):
$chargeArr = $charge->__toArray(true);
add true as argument for recursion
(Note: you should edit your question to remove your secret key and replace it with e.g. 'sk_test_...'. Even if it's just a test key, secret keys are not meant to be shared.)
The property you're trying to access is the cardholder's name. The source attribute of a charge object is actually a card object:
php > echo get_class($charge);
Stripe\Charge
php > echo get_class($charge->source);
Stripe\Card
So to get the cardholder's name, all you have to do is:
$name = $charge->source->name;
Note that name is an optional attribute for cards, so it's possible that you'll get NULL, an empty string, an email address, etc.