I want to create multidimensional array inside function and then access it outside function. Right now I have this function:
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;
}
and it is called inside this code:
$products_arr = array();
if ( $products->have_posts() ) : while ( $products->have_posts() ) : $products->the_post();
custom_shop_array_create($product);
endwhile;
endif;
the problem is that I cant access $products_arr
. I have tried replacing custom_shop_array_create($product);
with $my_array[] = custom_shop_array_create($product);
but then I get 3 dimensional array. So is there any way to get 2 dimensional array that would look like this:
product 1 (id,title,price,image)
product 2 (id,title,price,image) etc.
outside of the function.
Thanks in forward