PHP:使用循环从多个stdclass对象的数组中提取值

时间:2016-04-01 11:47:35

标签: php arrays object stdclass

        `array(2) {
  ["result"]=>
  object(stdClass)#6 (2) {
    ["totalResults"]=>
    int(10514658)
    ["products"]=>
    array(20) {
      [0]=>
      object(stdClass)#7 (13) {
        ["lotNum"]=>
        int(1)
        ["packageType"]=>
        string(5) "piece"
        ["imageUrl"]=>
        string(141) "http://g01.a.alicdn.com/kf/HTB1v_x3LpXXXXbDXFXXq6xXFXXXs/Medium-Super-color-Lace-up-Zapato-Mujer-grid-print-font-b-Shoes-b-font-The-Trend.jpg"
        ["30daysCommission"]=>
        string(8) "US $0.02"
        ["productId"]=>
        float(32515275585)
        ["discount"]=>
        string(2) "0%"
        ["originalPrice"]=>
        string(9) "US $41.99"
        ["productUrl"]=>
        string(48) "http://www.aliexpress.com/item//32515275585.html"
        ["productTitle"]=>
        string(128) "Medium, Super color Lace-up Zapato Mujer grid print Shoes The Trend of Female Soft Leisure Sapatos chaussure femme winter basket"
        ["validTime"]=>
        string(10) "2016-04-15"
        ["volume"]=>
        string(1) "1"
        ["evaluateScore"]=>
        string(3) "5.0"
        ["salePrice"]=>
        string(9) "US $41.99"
      }
      [1]=>
      object(stdClass)#8 (13) {
        ["lotNum"]=>
        int(1)
        ["packageType"]=>
        string(5) "piece"
        ["imageUrl"]=>
        string(134) "http://g02.a.alicdn.com/kf/HTB1Llk.IpXXXXasXpXXq6xXFXXXH/All-match-Women-font-b-shoes-b-font-high-canvas-font-b-shoes-b-font-women.jpg"
        ["30daysCommission"]=>
        string(8) "US $0.39"
        ["productId"]=>
        int(944204198)
        ["discount"]=>
        string(2) "0%"
        ["originalPrice"]=>
        string(9) "US $32.40"
        ["productUrl"]=>
        string(46) "http://www.aliexpress.com/item//944204198.html"
        ["productTitle"]=>
        string(118) "Hot selling! lady high canvas shoes summer & winter casual shoes sneakers for women Color block decoration top quality"
        ["validTime"]=>
        string(10) "2016-04-25"
        ["volume"]=>
        string(2) "18"
        ["evaluateScore"]=>
        string(3) "4.9"
        ["salePrice"]=>
        string(9) "US $32.40"
      }
      [2]=>
      object(stdClass)#9 (13) {
        ["lotNum"]=>
        int(1)
        ["packageType"]=>
        string(5) "piece"
        ["imageUrl"]=>
        string(144) "http://g03.a.alicdn.com/kf/HTB1N..tKXXXXXc3XXXXq6xXFXXXV/Slimming-font-b-shoes-b-font-women-fashion-leather-casual-font-b-shoes-b-font-women.jpg"
        ["30daysCommission"]=>
        string(8) "US $1.14"
        ["productId"]=>
        float(32426274938)
        ["discount"]=>
        string(2) "0%"
        ["originalPrice"]=>
        string(9) "US $27.04"
        ["productUrl"]=>
        string(48) "http://www.aliexpress.com/item//32426274938.html"
        ["productTitle"]=>
        string(101) "Free Shipping!Slimming sneakers for women, lady's Fitness Shoes Trendy Health Lady Beauty Swing Shoes"
        ["validTime"]=>
        string(10) "2016-04-11"
        ["volume"]=>
        string(2) "55"
        ["evaluateScore"]=>
        string(3) "4.8"
        ["salePrice"]=>
        string(9) "US $27.04"
      }
      [3]=>
      object(stdClass)#10 (13) {
        ["lotNum"]=>
        int(1)
        ["packageType"]=>
        string(5) "piece"
        ["imageUrl"]=>
        string(160) "http://g02.a.alicdn.com/kf/HTB1t5a9KFXXXXcdXXXXq6xXFXXXN/Hot-Fashion-Women-Genuine-Leather-font-b-Shoes-b-font-Height-Increasing-Platform-Wedge-Sneakers-Low.jpg"
        ["30daysCommission"]=>
        string(8) "US $0.06"
        ["productId"]=>
        int(1717026606)
        ["discount"]=>
        string(2) "0%"
        ["originalPrice"]=>
        string(9) "US $49.58"
        ["productUrl"]=>
        string(47) "http://www.aliexpress.com/item//1717026606.html"
        ["productTitle"]=>
        string(123) "2014 Summer Cutout Shoes Velcro Elevator Color Block Decoration Sneaker Shoes Sport Shoes Casual High-Top Women's Cool Boot"
        ["validTime"]=>
        string(10) "2016-04-01"
        ["volume"]=>
        string(1) "2"
        ["evaluateScore"]=>
        string(3) "5.0"
        ["salePrice"]=>
        string(9) "US $49.58"
      }

    }
  }
  ["errorCode"]=>
  int(20010000)
}`
  

这是响应即时通讯我希望获得键的基础中的值使用每个4个对象的循环我尝试了许多解决方案发布在这里,但没有人工作..

需要帮助请回复! 提前谢谢

2 个答案:

答案 0 :(得分:0)

$productsTitles = array();
foreach ($result->result->products as $product) {
    $productsTitles[] = $product->productTitle;
}
var_dump($productsTitles);

答案 1 :(得分:0)

基本上你在这里有一个很好的混合数组和对象。你的2个元素的外部数组包含你关心的“结果”和“errorCode”。

首先你需要遍历你拥有的每个结果,然后你需要找到所需的字段(根据评论“imageURL”)并将其保存在某个地方(或者回显它,无关紧要)。

我假设你的对象数组被称为$result

$imageUrls = [];
if (isset($result["results"]) && isset($result["results"]->products) {
    foreach ($result["results"]->products as $productObject) {
         $imageUrls[] = $productObject->imageUrl;
    }
}

print_r($imageUrls); // This just dumps the output, you can do whatever else you want with it.