嗨我有一个带有此值的cookie
a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}i:1;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:4617;s:11:"category_id";s:3:"211";s:5:"price";d:1200;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:74:"Машка блуза,EXTERRA ГТЦ 1 спрат и CITY MALL 1спрат";s:12:"product_name";s:0:"";s:11:"thumb_image";s:42:"thumb_e069100cef8553637664fc695df55c66.JPG";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}
我需要阅读该值,并在“product_id”之后的文本中获取位于数字5196位置的任何数字;一世: 。所以任何人都知道如何使用php获得数字5196?
答案 0 :(得分:2)
如果可能的话,我会非常反对直接使用序列化数据。相反,我会使用unserialize
反序列化数据,然后从结果数组中获取值。
$data = 'a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196...';
$rslt = unserialize($data);
echo $rslt[0]["product_id"];
答案 1 :(得分:1)
该字符串是序列化对象。如果你反序列化它,你可以遍历它并找到具有product_id = 5196的数组,并用它做你想做的事,例如获得其他值:
$str = 'a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}i:1;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:4617;s:11:"category_id";s:3:"211";s:5:"price";d:1200;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:74:"Машка блуза,EXTERRA ГТЦ 1 спрат и CITY MALL 1спрат";s:12:"product_name";s:0:"";s:11:"thumb_image";s:42:"thumb_e069100cef8553637664fc695df55c66.JPG";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}';
$unserialObj = unserialize($str);
foreach($unserialObj as $item) {
if($item['product_id'] == 5196) {
echo $item['cateogry_id'] . ', ';
echo $item['price'];
//etc
}
}
答案 2 :(得分:0)
$str = 'a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}i:1;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:4617;s:11:"category_id";s:3:"211";s:5:"price";d:1200;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:74:"Машка блуза,EXTERRA ГТЦ 1 спрат и CITY MALL 1спрат";s:12:"product_name";s:0:"";s:11:"thumb_image";s:42:"thumb_e069100cef8553637664fc695df55c66.JPG";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}';
$ar = explode(";",$str);
$ar2 = explode(":",$ar[4]);
print_r($ar2[1]);
答案 3 :(得分:0)
使用正则表达式:
$cookie = 'value from your post';
preg_match('/.*"product_id";i:([^;]+)/', $cookie, $matches);
$product_id = $matches[1];