我正在尝试从表woocommerce
中读取wp_options
中已弃用的购物车详情。字符串格式低于
a:19:{s:4:"cart";s:308:"a:1:{s:32:"d3d9446802a44259755d38e6d163e820";a:9:{s:10:"product_id";i:10;s:12:"variation_id";s:0:"";s:9:"variation";a:0:{}s:8:"quantity";i:1;s:10:"line_total";d:189;s:8:"line_tax";i:0;s:13:"line_subtotal";i:189;s:17:"line_subtotal_tax";i:0;s:13:"line_tax_data";a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}}}";s:15:"applied_coupons";s:6:"a:0:{}";s:23:"coupon_discount_amounts";s:6:"a:0:{}";s:27:"coupon_discount_tax_amounts";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:19:"cart_contents_total";d:189;s:20:"cart_contents_weight";i:2;s:19:"cart_contents_count";i:1;s:5:"total";i:0;s:8:"subtotal";i:189;s:15:"subtotal_ex_tax";i:189;s:9:"tax_total";i:0;s:5:"taxes";s:6:"a:0:{}";s:14:"shipping_taxes";s:6:"a:0:{}";s:13:"discount_cart";i:0;s:14:"shipping_total";i:0;s:18:"shipping_tax_total";i:0;s:9:"fee_total";i:0;s:4:"fees";s:6:"a:0:{}";}
如何拆分此值以阅读购物车和产品详情。
答案 0 :(得分:2)
您的字符串已序列化。你必须反序列化它。您可以使用在线工具。
或者,您可以运行PHP代码;
<?php
$serialized = 'a:19:{s:4:"cart";s:308:"a:1:{s:32:"d3d9446802a44259755d38e6d163e820";a:9:{s:10:"product_id";i:10;s:12:"variation_id";s:0:"";s:9:"variation";a:0:{}s:8:"quantity";i:1;s:10:"line_total";d:189;s:8:"line_tax";i:0;s:13:"line_subtotal";i:189;s:17:"line_subtotal_tax";i:0;s:13:"line_tax_data";a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}}}";s:15:"applied_coupons";s:6:"a:0:{}";s:23:"coupon_discount_amounts";s:6:"a:0:{}";s:27:"coupon_discount_tax_amounts";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:19:"cart_contents_total";d:189;s:20:"cart_contents_weight";i:2;s:19:"cart_contents_count";i:1;s:5:"total";i:0;s:8:"subtotal";i:189;s:15:"subtotal_ex_tax";i:189;s:9:"tax_total";i:0;s:5:"taxes";s:6:"a:0:{}";s:14:"shipping_taxes";s:6:"a:0:{}";s:13:"discount_cart";i:0;s:14:"shipping_total";i:0;s:18:"shipping_tax_total";i:0;s:9:"fee_total";i:0;s:4:"fees";s:6:"a:0:{}";}';
$unserialized = unserialize( $serialized );
var_dump( $unserialized );
?>
答案 1 :(得分:2)
您正在显示的字符串已序列化。 Serializing是一种将PHP值(在本例中为数组)及其类型和结构存储为字符串的方法。
您可以使用PHP的unserialize
函数将字符串转换回PHP值:
$value = unserialize($value);
这将返回一个包含cart
键的数组,由于某种原因,该数组也被序列化,因此您必须再次使用unserialize
:
$cart = unserialize($value['cart']);
现在您将能够访问购物车数据。
echo $cart['d3d9446802a44259755d38e6d163e820']['product_id']; // 10