【问题标题】:PHP: extract values from array of multiple stdclass objects using loopPHP:使用循环从多个stdclass对象的数组中提取值
【发布时间】:2016-04-01 11:47:34
【问题描述】:
        `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 个对象使用一个循环来获取键基中的值,我在这里尝试了许多解决方案,但没有人在工作..

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

【问题讨论】:

  • 请更具体地说明您想要的输出应该是什么。也让我们知道您尝试了什么。
  • 我想从每个 std 类中获取“imageUrl”的值..

标签: php arrays object stdclass


【解决方案1】:

基本上你在这里得到了数组和对象的完美混合。您的 2 个元素的外部数组包含您关心的“结果”和“错误代码”。

首先,您需要遍历您拥有的每个结果,然后您需要找到所需的字段(根据评论为“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.

【讨论】:

    【解决方案2】:
    $productsTitles = array();
    foreach ($result->result->products as $product) {
        $productsTitles[] = $product->productTitle;
    }
    var_dump($productsTitles);
    

    【讨论】:

    • 致命错误:不能在 C:\xampp\htdocs\ali\index.php 行中使用 stdClass 类型的对象作为数组 30
    • foreach ($productListByKeyword['result']->products as $product) { echo '
      --------- ' 。 $产品->产品标题。 ' ----------'; foreach ($product as $key => $value) { echo $key . ' = ' 。 $价值。 '
      '; } } 使用这段代码,但我上面提到的同样的错误
    • $productListByKeyword['result']->producs 你这里有一个错字:products
    • 它没有进入循环错误出现在第一行并且它没有进入第一个循环..错误是相同的不能使用 stdClass 类型的对象作为数组.. :( 我可能会请求你的帮助再多一点,我们将有一个解决方案..
    • 你是那个男人 :) 非常感谢你真的非常好,我很高兴再次感谢你 :)
    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 2014-06-18
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多