如何从“primary_image_url” - 141295获取产品ID

时间:2018-04-04 09:05:14

标签: php jquery json

我需要从“primary_image_url”获取此“141295”值 - 如何从图片网址中提到的“primary_image_url”获取此产品ID?

{
   "widgetTitle":"Recent History  For You",
   "title_text":"Recent History",
   "title":"Recent History",
   "items":[
      {
         "item_type":"product",
         "adunit_id":null,
         "name":"Dove Rejuvenated Volume Conditioner (240 ml)",
         "slug":"dove-rejuvenated-volume-conditioner-240-ml-86",
         "itemurl":"\/product\/dove-rejuvenated-volume-conditioner-240-ml-86",
         "category_name":"Conditioner",
         "stock_status":"1",
         "p_group":null,
         "thumb_image_url":"\/\/media6.ppl-media.com\/tr:h-235,w-235,c-at_max\/static\/img\/product\/141295\/dove-rejuvenated-volume-conditioner-240-ml_4_display_1476255016_7fe66d85.jpg",
         "primary_image_url":"\/\/media6.ppl-media.com\/tr:h-550,w-550,c-at_max\/static\/img\/product\/141295\/dove-rejuvenated-volume-conditioner-240-ml_4_display_1476255016_7fe66d85.jpg",
         "avg_rating":4,
         "total_rating":"13",
         "product_seller_id":"126619",
         "seller_id":"321",
         "price":"250",
         "mrp":"250",
         "offer_price":"200",
         "our_price":"200",
         "specialofferPrice":"200",
         "discount":"20",
         "offer_discount":0,
         "total_discount":20,
         "offer_text":"",
         "discount_price":50,
         "isincart":0,
         "iscart":0,
         "incart":0,
         "isliked":0
      },
      {
         "item_type":"product",
         "id":"107816",
         "adunit_id":null,
         "name":"Schwarzkopf Gliss Hair Repair With Liquid Keratin Serum Deep Repair Conditioner (200 ml)",
         "slug":"schwarzkopf-gliss-hair-repair-with-liquid-keratin-serum-deep-repair-conditioner-200-ml",
         "itemurl":"\/product\/schwarzkopf-gliss-hair-repair-with-liquid-keratin-serum-deep-repair-conditioner-200-ml",
         "category_name":"Conditioner",
         "stock_status":"1",
         "p_group":null,
         "thumb_image_url":"\/\/media6.ppl-media.com\/tr:h-235,w-235,c-at_max\/static\/img\/product\/107816\/schwarzkopf-gliss-hair-repair-with-liquid-keratin-serum-deep-repair-conditioner-200-ml_1_display_1506591377_c984d6fc.jpg",
         "primary_image_url":"\/\/media6.ppl-media.com\/tr:h-550,w-550,c-at_max\/static\/img\/product\/107816\/schwarzkopf-gliss-hair-repair-with-liquid-keratin-serum-deep-repair-conditioner-200-ml_1_display_1506591377_c984d6fc.jpg",
         "avg_rating":2.5,
         "total_rating":"3",
         "product_seller_id":"97130",
         "seller_id":"276",
         "price":"295",
         "mrp":"295",
         "offer_price":"266",
         "our_price":"266",
         "specialofferPrice":"266",
         "discount":"10",
         "offer_discount":0,
         "total_discount":10,
         "offer_text":"",
         "discount_price":29,
         "isincart":0,
         "iscart":0,
         "incart":0,
         "isliked":0
      }
   ],
   "type":"Recent",
   "x_id":"1",
   "widget_id":"rh",
   "ictk":0,
   "cr":"",
   "if_from_cache":0,
   "version":"v_1",
   "widget_name":"rh",
   "reco_type":"rh",
   "widget_type":"product",
   "widget_view_type":"product",
   "text":"History",
   "page":"product",
   "status":"success"
}

2 个答案:

答案 0 :(得分:1)

你可以使用PHP函数explode(),你可以阅读更多here

答案 1 :(得分:1)

我使用了正则表达式,首先根据product/拆分字符串然后找到数字。

$jsonArr = json_decode($json, true);

$chars = preg_split('/product\//', $jsonArr['items'][0]['primary_image_url']);
preg_match('/^\d+/', $chars[1], $matches);
print_r($matches);
$chars2 = preg_split('/product\//', $jsonArr['items'][1]['primary_image_url']);
preg_match('/^\d+/', $chars2[1], $matches2);
print_r($matches2);

Demo