0

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

1 Answer 1

1

Sure. Make your function return a row of the final array and do the appending yourself:

function custom_shop_array_create($product, $counter){
    $product_id = get_the_ID();
    $product_title = get_the_title($product_id);
    return [
        'id' => $product_id,
        'title' => $product_title,
        // etc
    ];
}

And then:

$products_arr = array();
if ( $products->have_posts() ) :
    while ( $products->have_posts() ) : 
        $products->the_post();
        $products_arr[] = custom_shop_array_create($product);
    endwhile;
endif;

That said, something strange is going on in the while loop. What does $products->the_post() do? Where is $product coming from?

5
  • it is comming from woocommerce plugin. It is eshop and product is some sort of global variable. I havent dug in their code so I dont know where exactly doest it come from but it is working so thats enough for me :) Commented Jul 23, 2013 at 9:02
  • are you sure that return should look like yours return? Because dreamweaver is saying that there is syntax error in return statement. Commented Jul 23, 2013 at 9:03
  • @horin: That is PHP 5.4 array syntax, you can replace [ ... ] with array( ... ) if it bothers you. BTW, Dreamweaver is -- let's say not the best tool to write code with. Commented Jul 23, 2013 at 9:07
  • yes I have replaced it with array(). Probably my server doesnt support PHP 5.4. I have one side question: which tool do you use to write code because I doesnt like dreamweaver too but I dont know about any other good tool Commented Jul 23, 2013 at 9:09
  • @horin: Dreamweaver does not understand the syntax. Your server might very well support it. I use PhpStorm for large-scale work and Sublime Text for the small stuff. Commented Jul 23, 2013 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.