Warning: implode(): Invalid arguments passed in functions.php on line 674 - php

i tried to go online after finishing the website but a error occurs
Warning: implode(): Invalid arguments passed in functions.php on line 674
foreach ( $one_array_font as $font => $variants ) {
$font = str_replace(' ', '+', $font);
$variants = implode(',', array_values($variants['variant']) );
$all_final_fonts[] = $font.':'.$variants;
}
$gfont = implode('|', $all_final_fonts); /* <-- This line fails */
wp_enqueue_style( 'zn_all_g_fonts', '//fonts.googleapis.com/css?family='.$gfont.''.$subset);
if ( $data['zn_main_style'] == 'dark' ) {
wp_enqueue_style('zn-dark-style', get_template_directory_uri() . '/css/dark-theme.css',array() ,false,'all');
}
if ( !empty ( $data['g_fonts_subset'] ) ) {
$subset = '&subset='.str_replace( ' ' , '' , $data['g_fonts_subset']);
}

Not really enough info in the question, but this is what I think is happening:
Firstly, $one_array_font is empty.
This means that the foreach() loop is never run.
This means that the line $all_final_fonts[] = $font.':'.$variants; is never run.
I'm guessing that $all_final_fonts was not defined earlier. Therefore it is still undefined when the code gets to the implode.
The implode() fails because it requires the input field to be an array, but you've given it an undefined variable.
Solution
Ensure that $all_final_fonts is defined regardless, by adding the following line before the foreach() loop:
$all_final_fonts = array();
This will initialise the variable as an array, so that implode() won't complain about it if you don't have any data.
Hope that helps.

You are seeing that warning because $all_final_fonts is not an array.
See http://php.net/manual/en/function.implode.php
regards

Related

These errors are not resolving even there's no mistake it is not perfoming reorder

Warning: array_key_exists() expects parameter 2 to be array, string given in D:\xampp\htdocs\ecom\customer\data.php on line 45
Notice: Array to string conversion in D:\xampp\htdocs\ecom\customer\data.php on line 51
My Code
$qry = dbQuery("SELECT `orders_product`.`pd_id`, `op_quantity` FROM `orders_product`
INNER JOIN `products_detail` ON `products_detail`.`pd_id`=`orders_product`.`pd_id`
WHERE orders_product.order_id = $id");
if(dbNumRows($qry)>0) {
$cart="";
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
$result = dbFetchArray($qry);
while($result = dbFetchArray($qry)) {
$product_detail_id = $result["pd_id"];
$order_quantity = $result["op_quantity"];
// echo $product_detail_id;
#print_r($result);
if(array_key_exists($product_detail_id, $_SESSION['cart'])){
$quantity = $_SESSION['cart'][$product_detail_id]['quantity'] + $order_quantity;
$_SESSION['cart'][$product_detail_id]['quantity'] = $quantity;
} else {
$cart = array("pd_id"=>$product_detail_id, "quantity"=> $order_quantity);
$_SESSION['cart'][$product_detail_id] = $cart;
print_r($cart);
}
}
}
$return = 1;
echo json_encode($return);
}
First problem
Warning: array_key_exists() expects parameter 2 to be array, string given in D:\xampp\htdocs\ecom\customer\data.php on line 45
That's because you use $_SESSION['cart'] with function array_key_exists. So when a person enters the website for the first time the variable $_SESSION['cart'] is not set.
What you do then is assign an empty string.
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
So then you use empty string with function array_key_exists.
Solution
To resolve this issue simply change
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
to
if(!isset($_SESSION['cart'])) { $_SESSION['cart']=array();}
Second problem
Notice: Array to string conversion in D:\xampp\htdocs\ecom\customer\data.php on line 51
Now, this is only a notice so that's not a big issue. It may not even be something wrong. After you change to not display errors it won't affect your website in any way.
But why is this showing? Again the same reason as with the previous problem. You assign empty string to variable $_SESSION['cart'] but then you treat is as an array
$_SESSION['cart'][$product_detail_id] = $cart;
When you change the assignment from empty string to empty array this issue will be solved as well.
By the way when you write a one line if statement a good practice is not to use the braces
So I would write it like
$_SESSION['cart'] = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
Or even better
$_SESSION['cart'] = $_SESSION['cart'] ?? [];
But this is only php 7.0 and higher
I hope you understand now what was wrong and why. If you have any questions feel free to ask. I'll try to reply the best away I can.

PHP Warnings: Parameter 2 and Invalid argument supplied foreach()

I am new to code and trying to identify errors on my site. It was created by a company using Php 5.6 and is now running Php 7.3
The debug file has two warnings (I've replaced the specific location with ...):
PHP Warning: Parameter 2 to WPE\Site_Preview::the_posts() expected to
be a reference, value given in ...wp-includes/class-wp-hook.php on
line 288 PHP Warning: Invalid argument supplied for foreach() in
...wp-content/mu-plugins/wpengine-common/plugin.php on line 1021
I've tried looking up the answers here, and the foreach error may be because it's not an array? But I'm not sure what an array is exactly, or how to check for it. Any suggestions and advice would be gratefully received, sorry I'm new to this and teaching myself!
the code for the first error (Parameter 2 expected to be a reference)
// Avoid the array_slice if possible.
if ( $the_['accepted_args'] == 0 ) {
$value = call_user_func( $the_['function'] );
} elseif ( $the_['accepted_args'] >= $num_args ) {
$value = call_user_func_array( $the_['function'], $args );
} else {
$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
The code for the second error (invalid argument supplied foreach):
// Some paths might reject CDN completely -- if so, don't do CDN replacements.
// In fact, UNDO any that were done by W3TC!
$undo_cdn = false;
foreach ( $wpe_no_cdn_uris as $re ) {
if ( preg_match( '#' . $re . '#', $uri ) ) {
$cdn_enabled = false;
$undo_cdn = true;
break;

explode() offset and delimiter warnings when delimiter is valid

I'm having a heck of a time trying to figure out what is going on. I have a block of code that is throwing some errors. Outside of the Warnings and Notices the code works fine and the arrays are not empty. But why I am getting those warnings and notices is bothersome. In my test environment I don't get the errors however when put on a different server they pop right up. I am stumped as to why. The lines of code that are throwing the errors and the errors being thrown are as follows
$libTitle = explode($libYearRes[0], $value);
Notice: Undefined offset: 0
Warning: explode(): Empty delimiter
$sortLibYearRes = explode(' ', $libYearRes[0]);
Notice: Undefined offset: 0
$libMovieRes = $sortLibYearRes[1];
Notice: Undefined offset: 1
The entire block of code is as follows with the problem lines denoted with a # in them
function checkMovieLibrary($movieTitle,$movieLibrary){
foreach ($movieLibrary as $value) {
preg_match('/\(\d\d\d\d\)\s\d\d\d*[Pp]/', $value, $libYearRes);
$libTitle = #explode($libYearRes[0], $value);
$libMovieTitle = rtrim($libTitle[0]);
$sortLibYearRes = #explode(' ', $libYearRes[0]);
//list($libMovieYear,$libMovieRes) = explode(' ', $libYearRes[0], 2);
$libMovieYear = $sortLibYearRes[0];
$libMovieRes = #$sortLibYearRes[1];
preg_match('/\(\d\d\d\d\)\s\d\d\d*[Pp]/', $movieTitle, $newfileYearRes);
$newFileTitle_array = explode($newfileYearRes[0], $movieTitle);
$newFileTitle = rtrim($newFileTitle_array[0]);
$sortFileYearRes = explode(' ', $newfileYearRes[0]);
$newFileMovieYear = $sortFileYearRes[0];
$newFileMovieRes = $sortFileYearRes[1];
if(stripos($libMovieTitle.' '.$libMovieYear, $newFileTitle.' '.$newFileMovieYear) !== false){
$moviesList_array[] = array('OriginalFile' => $value, 'NewFile' => $movieTitle);
return($moviesList_array);
}
}
return false;
}
//$movieTitle = "10 Cloverfield Lane (2016) 1080p.mkv"; //This is an example of $movieTitle string
//$movieLibrary = array('10 Cloverfield Lane (2016) 1080p.mkv','The Goonies (1985) 1080p.mp4'); this is an example of the $movieLibrary array
$doesMovieExist = checkMovieLibrary($Movie,$movieLibrary);
if(is_array($doesMovieExist) && $doesMovieExist !== false){
foreach($doesMovieExist as $fileKey => $fileValue) {
$originalFile = $fileValue['OriginalFile'];
$newFile = $fileValue['NewFile'];
}
//do some stuff here
}
First of all dont use # because it will bypass the error message. So user error_reporting(E_ALL) till development phase.
Then you are getting undefined offset it means your array is empty of does not exist so put a condition to check if it exist then the explode should work. It must be like that :
if(!empty($libYearRes[0]) || isset($libYearRes[0])) {
$libTitle = #explode($libYearRes[0], $value);
}
Undefined offset error means you're referring an array key that does not exist. If you like to see the array before processing it, use variable dump (var_dump) then you will see these array keys are not available for the given scenario.
you can avoid this error by checking array key really exist or not by using isset function. as example
if(isset($libYearRes[0])){ // will be true only if $libYearRes[0] is exist
$libTitle = explode($libYearRes[0], $value);
}

PHP / WordPress Warning: implode() [function.implode]: Invalid arguments passed

I'm using the Advanced Custom Fields Pro plugin and its Flexible Fields
I'm getting the following PHP warning for each layout related to the implode I'm using on the $displayCat variable:
Warning: implode() [function.implode]: Invalid arguments passed in /server-path/wp-content/themes/theme-name/page-home.php on line XX
I thought it was because $displayCat wasn't always an array, so tried to put $displayCat = array(); but that didn't eliminate the warnings.
Any ideas?
if( have_rows('home_content') ):
// loop through the rows of data
while ( have_rows('home_content') ) : the_row();
// 1x1 Nav
if( get_row_layout() == '1x1_nav' ):
$img = get_sub_field('img');
$alt = get_sub_field('alt');
$url = get_sub_field('url');
$displayCat = implode('" "', get_sub_field('display_cat'));
get_sub_field() doesn't return an array...it returns a string. Since you're trying to make an array out of individual strings, you should do the following:
$displayCat[] = get_sub_field('display_cat');
This will append each 'display_cat' sub-field to the $displayCat array.
Then, outside of your while() loop, you can implode(' ', $displayCat);

Uninitialized string offset while building array

I am getting the following error from the method presented below:
Notice: Uninitialized string offset: 5 in /path/to/file.php on line 30 Fatal error: Cannot access empty property in path/to/file.php on line 30
private function parse($xml, $index = '') {
echo count($xml->children()); //outputs 6
$count = 0;
foreach ($xml->children() as $key => $value) {
$this->$key[$count] = array();
$count++;
}
}
Any ideas why if I build an multi-dimensional in this way it results in an error?
If I change the assignment to:
$this->$key = array($count = > array());
This simply re-assigns the property each loop.
Thanks
Rich
Imagine you've got a string:
$string = 'abc`;
Doing substring access (which looks like array) will return you the character:
echo $string[2]; # c
Or you get your error when you're out of the index:
echo $string[3]; # null + warning
So now accessing a member of your object $this dynamically:
$this->$string[2]; # access $this->c
However this one breaks hardly:
$this->$string[3]; # access $this->null (not possible)
This gives you your fatal error of an empty property, a property with no name.
This explain what happens in your code, you have not told what you're trying to do so I hope this information will help you to continue with writing your parse function.
You should try to create the array before filling it.
I.e. $this->key = array();
That is, before looping through the XML elements.

Categories