2017-12-18 5 views
1

私に似たような似たような問題が数多くあり、いくつかの混合回答がありますが、私はどちらがうまくいくのか分かりませんが、私のforeachは私のjsonのapi私は取得しています。foreachのみ最初の結果を得る

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; } } 

私は私のforeachは結果をループされますが、そのは、最初のものだけを返すと、そのが他を返さない場合だけ見にecho '<a href="'.testlink.'">' . $producttz->product_name . '</a>';に追加もの。私はいくつかの解決策があることを知っていますが、私はどちらが私のために働くのかわかりません。

は、高度な

+0

最初のループの終わりはループを終了し、最初のデータセットを返す 'return $ data;'を行います。 –

+0

ありがとうございます!私は頭を叩いて、これらのスレッドをすべて読んで、私が間違っていたことを理解しました...それは簡単でした! – jgax87

答えて

4

でお願いしますreturn $data;は、最初の反復と最初のデータのみが返された直後にループを終了foreach()、内部にあるためです。

foreach()ループの外側に置いておいてください。

+1

もうまく動作します。私は実際に{}を削除して:を追加し、endforeachを追加しようとしました。それはあまりにも働いた...この事は笑を理解するために私に8時間かかった – jgax87

関連する問題