【问题标题】:foreach only getting first resultforeach 只得到第一个结果
【发布时间】:2018-06-01 04:11:41
【问题描述】:

我知道有很多类似的问题与我的相似,我得到了一些混合的答案,但我不确定哪一个适用于我的,但我的 foreach 只从我的 json api 返回一个项目得到。

JSON

{"categoryIndex":0,"page_count":2,"total_count":2,"product_list":[{"type":"EXTRACT","classifications":["SATIVA"],"brand":"BRAND ","description":"","active":true,"taxRate":0.0975,"test_result_url":"","attributes":{"total_doses":0,"mg_dose":"","cbd_ratio":"","total_cannabinoids_percentage":"","general":["ATTRIBUTE 1","ATTRIBUTE 2"],"flavors":["FLAVOR 2","FLAVOR 1"],"effects":["EFFECT 1","EFFECT 2"],"thc_percentage":"-.-","cbd_percentage":"-.-","terpenes_percentage":"-.-","size":"1G","medium":""},"images":{"large_image":"","cropped_image":""},"back_stock_inventory_quantity":510,"menu_title":"","last_update_date":"2017-12-18T09:52:36.842-08:00","secondary_tax_rate":0,"product_id":"39a928e2-ded1-11e7-8088-0a66b72be922","product_name":"STRAIN NAME","live_inventory_quantity":5,"sub_types":["FLOWER ROSIN"],"size_list":[{"barcodes":["dnny"],"type":"EXTRACT","size_id":"39b04857-ded1-11e7-8088-0a66b72be922","size":1,"live_inventory_quantity":5,"product_size_name":"STRAIN NAME","product_unit":"gram","primary_tax_rate":0.0975,"price_sell":36.446469}]},{"type":"MERCH","classifications":[],"brand":"","description":"","active":true,"taxRate":0.0975,"test_result_url":"","attributes":{"total_doses":0,"mg_dose":"","cbd_ratio":"","total_cannabinoids_percentage":"","general":[],"flavors":[],"effects":[],"thc_percentage":"-.-","cbd_percentage":"-.-","terpenes_percentage":"-.-","size":"MEN'S L","medium":""},"images":{"large_image":"","cropped_image":""},"back_stock_inventory_quantity":0,"menu_title":"","last_update_date":"2017-12-18T09:52:36.907-08:00","secondary_tax_rate":0,"product_id":"5bb24bec-e072-11e7-8088-0a66b72be922","product_name":"123","live_inventory_quantity":123,"sub_types":[],"size_list":[{"barcodes":["sLz3"],"type":"MERCH","size_id":"5bb946c2-e072-11e7-8088-0a66b72be922","size":1,"live_inventory_quantity":123,"product_size_name":"123","product_unit":"count","primary_tax_rate":0.0975,"price_sell":112.072893}]}]}

我的foreach

$response = wp_remote_get("xxx" );
    $body = wp_remote_retrieve_body( $response );

    $json = json_decode($body);


    /**** import products****/
    $product = array();
    $product_variations = array();



    if( ! empty( $json ) ) {

        foreach( $json->product_list as $key => $producttz ) {

            echo '<a href="'.testlink.'">' . $producttz->product_name . '</a>';


            $product[$key]['_product_id'] = (string) $producttz->product_name;
            $product[$key]['name'] = (string) $producttz->product_name;
            $product[$key]['description'] = (string) $producttz->product_name;
            $product[$key]['regular_price'] = (string) $producttz->price_sell;

            // Stock
            $product[$key]['manage_stock'] = (bool) $producttz->live_inventory_quantity;

            $data[$key]['products'] = $product;



            return $data; } }

我在那里添加了echo '&lt;a href="'.testlink.'"&gt;' . $producttz-&gt;product_name . '&lt;/a&gt;'; 只是为了查看我的 foreach 是否循环遍历结果,但它只返回第一个而不返回其他结果。我知道有一些解决方案,但我不确定哪一个适合我。

谢谢你的进步

【问题讨论】:

  • 第一个循环结束时会执行return $data;,这将终止循环并返回第一组数据。
  • 谢谢!我正在猛烈地阅读所有这些线程以找出我做错了什么......这很简单!

标签: php json wordpress api foreach


【解决方案1】:

这是因为return $data; 在foreach() 内部,它在第一次迭代后立即终止循环,只返回第一个数据。

把它放在foreach() 循环之外,你就可以开始了。

【讨论】:

  • 这也很好用,哈哈。我实际上尝试删除 { } 并添加 : 并添加 endforeach;它也起作用了……这件事我花了 8 个小时才弄明白,哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多