【问题标题】:How to get a specific variable from a complex php object?如何从复杂的 php 对象中获取特定变量?
【发布时间】:2019-08-03 09:57:01
【问题描述】:

我需要从 woocommerce 中的订阅对象中获取一个特定的值,然后将其放入一个变量中,但不幸的是我无法解决这个问题。

我从 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":3353,"key":"_cred_form_id","value":"156"}

我该怎么做?

如果有人能给我一个关于如何解决这个问题的解决方案,那就太好了。非常感谢!

【问题讨论】:

  • 这很简单,$yourObject->meta_data 将返回一个数组,然后检查该数组是否具有该键,然后检索它,否则为后备值,顺便说一句,您可以使用 $yourObject->meta_data[3]

标签: php wordpress object woocommerce


【解决方案1】:

此代码将按您的意愿执行。

$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;

来自 woocomerce 的数据是 json。所以我们只需要指向目标数据。

【讨论】:

    猜你喜欢
    • 2017-12-27
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多