Home > OS >  How to recognize string as array with echo
How to recognize string as array with echo

Time:04-14

I have a shortcode for woocommerce that shows all orders placed by a customer, it works. However, I came to a piece of code that gives me the following message:

Notice: Array to string conversion in /home/vwzidcur/public_html/wp-content/themes/astra-child/woocommerce/woo-shortcodes.php on line 67. On line 67 of my file i have echo.

Googling and reading the official php manual I came to the conclusion that the problem can be solved with implode or by iterating $downloads array with foreach. I tried but couldn't. I am relatively new to this, any help would be appreciated.

$customer = wc_get_orders(['customer_id' => get_current_user_id(),]);

foreach ( $customer as $order ) {
foreach ( $order->get_items() as $item_id => $item ) {

$downloads = $order->get_downloadable_items();

  if ($downloads){ 
  wc_get_template(
    'button-downloads-dashboard.php',
     array(
     'downloads'  => $downloads,
     ));
     }

echo '<div > '. $downloads .' </div>';
}}

CodePudding user response:

Try something like this;

// FIRST ALWAYS CHECK IF ITS AN ARRAY
if(is_array($downloads)){
  // START THE LOOP
  foreach($downloads as $product){
    // HERE YOU CHOOSE WHAT TO PRINT OF ALL THE DIFFERENT VARIABLES IN THE ARRAY
    echo '<div ><a href="'. $product['download_url'] .'" target="_blank">'. $product['product_name'] .' </a></div>';
  } // END LOOP
} // END IF ARRAY
  • Related