只获得第一个结果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了只获得第一个结果相关的知识,希望对你有一定的参考价值。
我知道有很多类似的问题与我的相似,我得到了一些混合的答案,但我不确定哪一个会与我合作,但我的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 '<a href="'.testlink.'">' . $producttz->product_name . '</a>';
只是为了看看我的foreach是否循环结果,但它只返回第一个而不返回其他的。我知道有一些解决方案,但我不确定哪一个适合我的。
先谢谢你
答案
这是因为return $data;
位于foreach()
内部,它在第一次迭代后立即终止循环,并且只返回第一个数据。
把它放在foreach()
循环之外,你会很高兴。
以上是关于只获得第一个结果的主要内容,如果未能解决你的问题,请参考以下文章