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


PHP: extract values from array of multiple stdclass objects using loop

        `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 个元素的外部数组包含您关心的"结果"和"错误代码"。

首先,您需要遍历您拥有的每个结果,然后您需要找到所需的字段(根据注释为"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.
$productsTitles = array();
foreach ($result->result->products as $product) {
    $productsTitles[] = $product->productTitle;
}
var_dump($productsTitles);