简体   繁体   English

如何从复杂的 php 对象中获取特定变量?

[英]How to get a specific variable from a complex php object?

I need to get a specific value from the subscription object in woocommerce to put it into a variable, but unfortunately I don't manage to solve this problem.我需要从 woocommerce 中的订阅对象中获取特定值以将其放入变量中,但不幸的是我无法解决这个问题。

This object I get from woocommerce subscription: {"id":327,"parent_id":326,"status":"expired","currency":"EUR","version":"3.6.5","prices_include_tax":false,"date_created":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"date_modified":{"date":"2019-08-03 10:49:23.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"discount_total":"0","discount_tax":"0","shipping_total":"0.00","shipping_tax":"0","cart_tax":"0","total":"2.00","total_tax":"0","customer_id":5,"order_key":"wc_order_O6jvDq6kygZxu","billing":{"first_name":"Hello","last_name":"Test","company":"","address_1":"Test 123","address_2":"","city":"1111","state":"","postcode":"8888","country":"AT","email":"123@test123.com","phone":"1234"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"stripe","payment_method_title":"Credit Card (Stripe)","transaction_id":"","customer_ip_address":"178.165.131.46","customer_user_agent":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/70.0.3538.77 Safari\\/537.36","created_via":"checkout","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"","billing_period":"day","billing_interval":"1","suspension_count":0,"requires_manual_renewal":false,"cancelled_email_sent":"","trial_period":"","schedule_trial_end":null,"schedule_next_payment":null,"schedule_cancelled":null,"schedule_end":{"date":"2019-08-03 10:49:24.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"schedule_payment_retry":null,"schedule_start":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"switch_data":"","number":"327","meta_data":[{"id":3350,"key":"is_vat_exempt","value":"no"},{"id":3351,"key":"_cred_meta","value":"a:1:{i:0;a:3:{s:15:\\"cred_product_id\\";s:3:\\"168\\";s:12:\\"cred_form_id\\";i:156;s:12:\\"cred_post_id\\";i:325;}}"},{"id":3352,"key":"_cred_post_id","value":"325"}, {"id":3353,"key":"_cred_form_id","value":"156"}, {"id":3373,"key":"_stripe_customer_id","value":"cus_FXnn7KOOi3LN6T"},{"id":3374,"key":"_stripe_source_id","value":"src_1F2iCEFxFOzrkjfff6wQYsA7"}],"line_items":{"31":{}},"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[]}我从 woocommerce 订阅中获得的这个对象: {"id":327,"parent_id":326,"status":"expired","currency":"EUR","version":"3.6.5","prices_include_tax":false,"date_created":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"date_modified":{"date":"2019-08-03 10:49:23.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"discount_total":"0","discount_tax":"0","shipping_total":"0.00","shipping_tax":"0","cart_tax":"0","total":"2.00","total_tax":"0","customer_id":5,"order_key":"wc_order_O6jvDq6kygZxu","billing":{"first_name":"Hello","last_name":"Test","company":"","address_1":"Test 123","address_2":"","city":"1111","state":"","postcode":"8888","country":"AT","email":"123@test123.com","phone":"1234"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"stripe","payment_method_title":"Credit Card (Stripe)","transaction_id":"","customer_ip_address":"178.165.131.46","customer_user_agent":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/70.0.3538.77 Safari\\/537.36","created_via":"checkout","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"","billing_period":"day","billing_interval":"1","suspension_count":0,"requires_manual_renewal":false,"cancelled_email_sent":"","trial_period":"","schedule_trial_end":null,"schedule_next_payment":null,"schedule_cancelled":null,"schedule_end":{"date":"2019-08-03 10:49:24.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"schedule_payment_retry":null,"schedule_start":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"switch_data":"","number":"327","meta_data":[{"id":3350,"key":"is_vat_exempt","value":"no"},{"id":3351,"key":"_cred_meta","value":"a:1:{i:0;a:3:{s:15:\\"cred_product_id\\";s:3:\\"168\\";s:12:\\"cred_form_id\\";i:156;s:12:\\"cred_post_id\\";i:325;}}"},{"id":3352,"key":"_cred_post_id","value":"325"}, {"id":3353,"key":"_cred_form_id","value":"156"}, {"id":3373,"key":"_stripe_customer_id","value":"cus_FXnn7KOOi3LN6T"},{"id":3374,"key":"_stripe_source_id","value":"src_1F2iCEFxFOzrkjfff6wQYsA7"}],"line_items":{"31":{}},"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[]} {"id":327,"parent_id":326,"status":"expired","currency":"EUR","version":"3.6.5","prices_include_tax":false,"date_created":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"date_modified":{"date":"2019-08-03 10:49:23.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"discount_total":"0","discount_tax":"0","shipping_total":"0.00","shipping_tax":"0","cart_tax":"0","total":"2.00","total_tax":"0","customer_id":5,"order_key":"wc_order_O6jvDq6kygZxu","billing":{"first_name":"Hello","last_name":"Test","company":"","address_1":"Test 123","address_2":"","city":"1111","state":"","postcode":"8888","country":"AT","email":"123@test123.com","phone":"1234"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"stripe","payment_method_title":"Credit Card (Stripe)","transaction_id":"","customer_ip_address":"178.165.131.46","customer_user_agent":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/70.0.3538.77 Safari\\/537.36","created_via":"checkout","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"","billing_period":"day","billing_interval":"1","suspension_count":0,"requires_manual_renewal":false,"cancelled_email_sent":"","trial_period":"","schedule_trial_end":null,"schedule_next_payment":null,"schedule_cancelled":null,"schedule_end":{"date":"2019-08-03 10:49:24.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"schedule_payment_retry":null,"schedule_start":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\\/Berlin"},"switch_data":"","number":"327","meta_data":[{"id":3350,"key":"is_vat_exempt","value":"no"},{"id":3351,"key":"_cred_meta","value":"a:1:{i:0;a:3:{s:15:\\"cred_product_id\\";s:3:\\"168\\";s:12:\\"cred_form_id\\";i:156;s:12:\\"cred_post_id\\";i:325;}}"},{"id":3352,"key":"_cred_post_id","value":"325"}, {"id":3353,"key":"_cred_form_id","value":"156"}, {"id":3373,"key":"_stripe_customer_id","value":"cus_FXnn7KOOi3LN6T"},{"id":3374,"key":"_stripe_source_id","value":"src_1F2iCEFxFOzrkjfff6wQYsA7"}],"line_items":{"31":{}},"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[]}

Now I want to read the "value" out of this line and put into my variable:现在我想从这一行中读取“值”并放入我的变量中:

{"id":3353,"key":"_cred_form_id","value":"156"}

How can I do that?我怎样才能做到这一点?

Would be great if somebody could give me a solution on how to solve this problem.如果有人能给我一个关于如何解决这个问题的解决方案,那就太好了。 Thanks a lot!非常感谢!

This code will do as you want.此代码将按您的意愿执行。

$json = '{"id":327,"parent_id":326,"status":"expired","currency":"EUR","version":"3.6.5","prices_include_tax":false,"date_created":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"date_modified":{"date":"2019-08-03 10:49:23.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"discount_total":"0","discount_tax":"0","shipping_total":"0.00","shipping_tax":"0","cart_tax":"0","total":"2.00","total_tax":"0","customer_id":5,"order_key":"wc_order_O6jvDq6kygZxu","billing":{"first_name":"Hello","last_name":"Test","company":"","address_1":"Test 123","address_2":"","city":"1111","state":"","postcode":"8888","country":"AT","email":"123@test123.com","phone":"1234"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"stripe","payment_method_title":"Credit Card (Stripe)","transaction_id":"","customer_ip_address":"178.165.131.46","customer_user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/70.0.3538.77 Safari\/537.36","created_via":"checkout","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"","billing_period":"day","billing_interval":"1","suspension_count":0,"requires_manual_renewal":false,"cancelled_email_sent":"","trial_period":"","schedule_trial_end":null,"schedule_next_payment":null,"schedule_cancelled":null,"schedule_end":{"date":"2019-08-03 10:49:24.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"schedule_payment_retry":null,"schedule_start":{"date":"2019-08-03 10:40:55.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"switch_data":"","number":"327","meta_data":[{"id":3350,"key":"is_vat_exempt","value":"no"},{"id":3351,"key":"_cred_meta","value":"a:1:{i:0;a:3:{s:15:\"cred_product_id\";s:3:\"168\";s:12:\"cred_form_id\";i:156;s:12:\"cred_post_id\";i:325;}}"},{"id":3352,"key":"_cred_post_id","value":"325"},
{"id":3353,"key":"_cred_form_id","value":"156"},
{"id":3373,"key":"_stripe_customer_id","value":"cus_FXnn7KOOi3LN6T"},{"id":3374,"key":"_stripe_source_id","value":"src_1F2iCEFxFOzrkjfff6wQYsA7"}],"line_items":{"31":{}},"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[]}';

$object = json_decode($json);

$meta_data = $object->meta_data;

$target_id = 3353;
foreach($meta_data as $data) {
    if ($data->id == $target_id) {
        $result = $data;
        break;
    }
}
echo $result->value;

Data from woocomerce is a json.来自 woocomerce 的数据是一个 json。 So we just need to pointing to target data.所以我们只需要指向目标数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM