0

I have something wrong with:

PHP Warning:  array_push() expects parameter 1 to be array, string given in

the code in function.php is:

global $wpdb;
    $i=0;
$key_count=array();
    
    $order_id  = $order->get_id();
    $order = wc_get_order($order_id);   
        foreach( $order->get_items() as $item_id => $item ){
    //Get the product ID
    $product_id = $item->get_product_id();
      $key_count[$i] = $wpdb->get_var("SELECT count(license_key) FROM bff_lmfwc_licenses where product_id='$product_id'and order_id is NULL");
      $i=$i+1;
        array_push($key_count[0],$key_count[$i]);
        }

Can you see my mistake?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
xb yang
  • 21
  • 2
  • Here array_push($key_count[0],$key_count[$i]) , instead of $key_count[0] try $key_count array_push() treats first argument as a stack, and pushes the passed variables onto the end of array https://www.php.net/manual/en/function.array-push.php – Manoj Agrawal Jul 24 '22 at 11:25
  • I don't understand why you need `array_push` here, you are setting `$key_count[$i]` already. – Jacob Mulquin Jul 24 '22 at 11:31
  • if ( is_a($order, 'WC_Order')&& min($key_count)>0 ) {} i want to use min($key_count)>0 this. – xb yang Jul 24 '22 at 11:39
  • `$key_count[0]` is not an array. Don't use array_push() at all. https://www.php.net/manual/de/function.array-push.php – Markus Zeller Jul 24 '22 at 11:43
  • @xbyang Please edit your code with the full code snippet – Jacob Mulquin Jul 24 '22 at 11:52

0 Answers0