0
我想在函數內創建多維數組,然後在函數外部訪問它。現在,我有這樣的功能:PHP - 在函數內部創建多維數組
function custom_shop_array_create($product, $counter){
$product_id = get_the_ID();
$product_title = get_the_title($product_id);
$products_arr[]['id'] = $product_id;
$products_arr[]['title'] = $product_title;
$products_arr[]['price'] = $product->get_price();
$products_arr[]['image'] = get_the_post_thumbnail($product_id, 'product-list-thumb', array('class' => 'product-thumbnail', 'title' => $product_title));
return $products_arr;
}
,它被稱爲在代碼裏:
$products_arr = array();
if ($products->have_posts()) : while ($products->have_posts()) : $products->the_post();
custom_shop_array_create($product);
endwhile;
endif;
的問題是,我不能訪問$products_arr
。我試過用$my_array[] = custom_shop_array_create($product);
代替custom_shop_array_create($product);
,但後來我得到3維陣列。那麼有什麼辦法可以得到二維數組,看起來像這樣:
product 1 (id,title,price,image)
product 2 (id,title,price,image) etc.
函數的外部。
謝謝轉發
它來自woocommerce插件。這是eshop,產品是某種全局變量。我沒有挖他們的代碼,所以我不知道它究竟從哪裏來,但它的工作對我來說足夠了:) – horin
你確定返回應該看起來像你的返回?因爲Dreamweaver在說return語句中有語法錯誤。 – horin
@horin:這是PHP 5.4數組語法,如果它困擾你,你可以用'array(...)'替換'[...]'。順便說一句,Dreamweaver是 - 讓我們說不是編寫代碼的最佳工具。 – Jon