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);
Related
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;
I have this bit of code:
if( array_key_exists('customtextcolor',$atts) ){
// If in array, then check if none, if not none, add to CSS classes
if ( 'none' !== $atts['customtextcolor'] ) {
$custstyles[] = $atts['customtextcolor'];
}
}
Which is on line 90, and my php.log shows an error on line 90.
Apr 2 08:46:16 #####.net ool www: PHP Warning: array_key_exists() expects parameter 2 to be array, string given in /var/www/####/wp-content/plugins/Fusion-Builder-Custom-Text-For-HP/fusion-builder-custom-text-for-hp.php on line 90
Which I thought was strange, because I was sure it is was an array, because the code works as expected. I also used echo gettype($atts); and it returns an array. When I var_dump($atts); it renders the full array.
Any idea what php.log is showing this error message?
If $atts is a string, why does gettype() identify it as an array? And why does var_dump() render it as an array?
I've looked at the other array_key_exists() questions on StackOverflow, but they aren't the same issue as far as I can tell.
Change the if condition as below:
if(is_array($atts) && array_key_exists('customtextcolor', $atts))
This will check if $atts is an array at first then will check the array_key_exist operation.
Just write the following line of code:
array_key_exists('customtextcolor', (array) $atts);
This will cast the $atts-variable into an array if it's not already an array.
You can also just check if there is an array with a value per:
isset($atts['customtextcolor']);
if(isset($_POST['price']))
{
$ret = array();
$price= $_POST['price'];
array_push($ret,$price);
$pr=count($ret);
for($i=0; $i>$pr;$i++)
{
$pri[]=$pr[$i]*$disount/100;
echo "<script>alert('$i'); </script>";
}
$nprice = implode("," , $pri);
}
else $nprice = '0';
when data is submitted it will get $_POST['price'] . In my code i m trying apply discount on $price.As i know discount is already set . but it is giving me error ! ) SCREAM: Error suppression ignored for
Warning: implode() [function.implode]: Invalid arguments passed
Your for loop is actually wrong.. Change it to
for($i=0; $i<$pr;$i++)
//^^ <--- Do this change..
Actually it should be less than operator..
You had greater than operator and thus the condition fails , so the control flow does not go inside of your for loop, hence the $pri array obviously won't be populated and thus leading to this error.
Initialize the $pri array before the if condition
$pri = array();
Also for the for loop, if $pr is an array the condition should be something like:
for($i=0; $i<count($pr);$i++)
$pri is undefined here, so NULL. And NULL is an invalid argument to implode.
for($i=0; $i>$pr;$i++)
You seem to have used the wrong comparison sign (< vs. >), so the loop is never entered and $pri never set to be an array.
Fix that and for safety write $pri = array(); before the loop to initialize it.
I am having situation where i want to pass variables in php function.
The number of arguments are indefinite. I have to pass in the function without using the array.
Just like normal approach. Comma Separated.
like test(argum1,argum2,argum3.....,..,,.,.,.....);
How i will call the function? Suppose i have an array array(1,2,3,4,5) containing 5 parameters. i want to call the function like func(1,2,3,4,5) . But the question is that, How i will run the loop of arguments , When calling the function. I tried func(implode(',',array)); But it is taking all return string as a one parameters
In the definition, I also want the same format.
I can pass variable number of arguments via array but i have to pass comma separated.
I have to pass comma separated. But at the time of passing i don't know the number of arguments , They are in array.
At the calling side, use call_user_func_array.
Inside the function, use func_get_args.
Since this way you're just turning an array into arguments into an array, I doubt the wisdom of this though. Either function is fine if you have an unknown number of parameters either when calling or receiving. If it's dynamic on both ends, why not just pass the array directly?!
you can use :
$function_args = func_get_args();
inside your test() function definition .
You can just define your function as
function test ()
then use the func_get_args function in php.
Then you can deal with the arguments as an array.
Example
function reverseConcat(){
return implode (" ", array_reverse(func_get_args()));
}
echo reverseConcat("World", "Hello"); // echos Hello World
If you truely want to deal with them as though they where named parameters you could do something like this.
function getDistance(){
$params = array("x1", "y1", "x2", "y2");
$args = func_get_args();
// trim excess params
if (count($args) > count($params) {
$args = array_slice(0, count($params));
} elseif (count($args) < count($params)){
// define missing parameters as empty string
$args = array_pad($args, count($params), "");
}
extract (array_combine($params, $args));
return sqrt(pow(abs($x1-$x2),2) + pow(abs($y1-$y2),2));
}
use this function:
function test() {
$args = func_get_args();
foreach ($args as $arg) {
echo "Arg: $arg\n";
}
}
I'm not sure what you mean by "same format." Do you mean same type, like they all have to be a string? Or do you mean they need to all have to meet some criteria, like if it's a list of phone numbers they need to be (ddd) ddd-dddd?
If it's the latter, you'll have just as much trouble with pre-defined arguments, so I'll assume you mean you want them all to be the same type.
So, going off of the already suggested solution of using func_get_args(), I would also apply array_filter() to ensure the type:
function set_names() {
function string_only($arg) {
return(is_string($arg));
}
$names_provided = func_get_args();
// Now you have an array of the args provided
$names_provided_clean = array_filter($names_provided, "string_only");
// This pulls out any non-string args
$names = array_values($names_provided_clean);
// Because array_filter doesn't reindex, this will reset numbering for array.
foreach($names as $name) {
echo $name;
echo PHP_EOL;
}
}
set_names("Joe", "Bill", 45, array(1,2,3), "Jane");
Notice that I don't do any deeper sanity-checks, so there could be issues if no values are passed in, etc.
You can use array also using explode http://www.php.net/manual/en/function.explode.php.
$separator = ",";
$prepareArray = explode ( $separator , '$argum1,$argum2,$argum3');
but be careful, $argum1,$argum2, etc should not contain , in value. You can overcome this by adding any separator. $separator = "VeryUniqueSeparator";
I don't have code so can't tell exact code. But manipulating this will work as your requirements.
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