PHP implode query - why is this wrong?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

PHP implode query - why is this wrong?

Post by simonmlewis »

Hi there.
We are getting errors on the implode code.
Is the current version of PHP using this differently now??
Error is PHP Warning: implode(): Invalid arguments passed.

Code: Select all

add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_echo_stock_variations_loop' );
 
function bbloomer_echo_stock_variations_loop(){
global $product;
    if ($product->get_type() == 'variable') {
        foreach ($product->get_available_variations() as $key) {
        $attr_string = '';
            foreach ( $key['attributes'] as $attr_name => $attr_value) {
                                $attr_string[] = $attr_value;
            }
            if ( $key['max_qty'] > 0 ) { echo '' . implode(', ', $attr_string) . ': ' . $key['max_qty'] .' in stock<br/>'; } else { echo '' . implode(', ', $attr_string) . ': out of stock<br/>'; }
            }
    }
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP implode query - why is this wrong?

Post by VladSun »

Code: Select all

$attr_string = '';
and if there are no $key['attributes'] items then you have an error (implode expects as 2nd param an array, not a string).

Try instead:

Code: Select all

$attr_string = [];
There are 10 types of people in this world, those who understand binary and those who don't
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP implode query - why is this wrong?

Post by simonmlewis »

Great thanks. Is it bad code or just old code?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP implode query - why is this wrong?

Post by VladSun »

simonmlewis wrote:Great thanks. Is it bad code or just old code?
Bad code:
0) by design - usage of global in a function;
1) by design - mixing business logic and views;
2) by implementation - initializing a variable as a string and later implicitly casted and used as an array.
3) Code formatting?
There are 10 types of people in this world, those who understand binary and those who don't
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP implode query - why is this wrong?

Post by simonmlewis »

Wow - ok. It was taken from a website called Business Bloomer.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply