I am getting this message:
PHP Notice: Undefined offset: 1 in
/home/printet1/public_html/wp-content/mu-plugins/gd-system-plugin/class-gd-system-plugin-config.php on line 56
The relevant code is:
public function get_config( ) {
if ( empty( $this->config ) ) {
$defaults = $this->_get_config( '/web/conf/gd-wordpress.conf' );
$resellers = $this->_get_config( '/web/conf/gd-resellers.conf' );
$reseller = null;
if ( defined( 'GD_RESELLER' ) && is_numeric( GD_RESELLER ) ) {
$reseller = $resellers[GD_RESELLER];
}
if ( is_array( $reseller ) && !empty( $reseller ) ) {
$this->config = array_merge( $defaults, $reseller );
} else {
$this->config = $defaults;
}
}
return $this->config;
}
With line 56 specifically being:
$reseller = $resellers[GD_RESELLER];
I'm relatively new to coding and would like any information/help on what I can do to fix this. I have read explanations on what is going wrong but don't understand what to do in order to address this issue.
Thanks in advance for any help!
It seems that $resellers array doesn't hold the value you are looking for.
one way to debug this, is to add
var_dump(GD_RESELLER);
var_dump($resellers);
to line 55, just before this line:
$reseller = $resellers[GD_RESELLER];
to get an idea of what's going on.
if 'GD_RESELLER' is a number, make sure that $resellers array has a value in that position. i.e:
if $reseller array looks like this:
array(3) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(5) "third" } string(5)
and GD_RESELLER constant equels '2', then asking for
$resellers[GD_RESELLER]
will return 'third'
remember that arrays in PHP use zero-based index,
so the first child of that array is in position '0' and not '1', etc.
See PHP's Docs about Arrays
Another simple solution, just edit bellow code in gd-config.php file.
define( 'GD_RESELLER', false);
Related
My goal is very simple, I want to parse the $GLOBALS variable in JSON (to log it). According to this Stackoverflow post, https://stackoverflow.com/a/23176085/1369579, I have to remove the recursive variable.
The following code works:
<?php
$global_array = $GLOBALS;
$index = array_search('GLOBALS',array_keys($global_array));
$json = json_encode(array_splice($global_array, $index, $index-1));
var_dump($json);
?>
It returns string(59) "{"GLOBALS":{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}}" (in http://sandbox.onlinephpfunctions.com)
But I have to use an intermediate variable to store the array_splice result. When I do this, I doesn't works:
<?php
$global_array = $GLOBALS;
$index = array_search('GLOBALS',array_keys($global_array));
$splice_result = array_splice($global_array, $index, $index-1);
var_dump(json_encode($splice_result));
?>
The result is bool(false) and the json_last_error_msg() returns Recursion detected.
What the difference between the two versions? I really don't understand. For me foo(bar()) is the exactly the same code than $bar = bar(); foo($bar)…
I just understood my problem, the call of array_splice remove the $GLOBALS variable… but I still don't understand why.
I tried to put my code into a function, because I thought my problem was to put the code directly in the global scope:
<?php
function globalWithoutGlobals() {
$global_array = $GLOBALS;
$index = array_search('GLOBALS',array_keys($global_array));
array_splice($global_array, $index, 1);
return $global_array;
}
var_dump(json_encode(globalWithoutGlobals()));
var_dump(json_encode(globalWithoutGlobals()));
/* returns
# First call: success,
string(47) "{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}"
# Second call : wtf ?!!
<br />
<b>Notice</b>: Undefined variable: GLOBALS in <b>[...][...]</b> on line <b>4</b><br />
*/
It's still very weird for me, the $global_array should be changed, not the $GLOBALS. To test this behaviour, I did the same thing on others arrays (with recursion too):
<?php
// Looks like $GLOBALS (with recursive element)
$globals_array = ["foo" => "bar"];
$globals_array['GLOBALS'] = &$globals_array;
$copy = $globals_array;
$index = array_search('GLOBALS', array_keys($copy));
array_splice($copy, $index, 1);
var_dump($globals_array);
var_dump($copy);
/* return:
array(2) {
["foo"]=>
string(3) "bar"
["GLOBALS"]=>
&array(2) {
["foo"]=>
string(3) "bar"
["GLOBALS"]=>
*RECURSION*
}
}
array(1) {
["foo"]=>
string(3) "bar"
}
*/
It returns the expected output, so why the behaviour is not the same with $GLOBALS ? 😩
To resolve my issue, I changed my approach and I stopped used array_splice, instead I just do a naive implementation with a foreach on the $GLOBALS and it works like expected:
<?php
function globalWithoutGlobals() {
$result = [];
foreach ($GLOBALS as $key => $value) {
if ($key !== 'GLOBALS') {
$result[$key] = $value;
}
}
return $result;
}
var_dump(json_encode(globalWithoutGlobals()));
var_dump(json_encode(globalWithoutGlobals()));
/* it returns:
string(47) "{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}"
string(47) "{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}"
*/
If someone know why the behaviour is different between my two first examples above. I'm curious to understand 😊
Context: On one of the sites where my WordPress plugin is installed I'm seeing a series of PHP warnings, but I'm not entirely sure why this is happening. I'm hoping someone here can help me figure out how to solve this warning.
Code Sample:
function my_function( $array ) {
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
The Warning:
Warning: Illegal string offset 'where' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 14
Warning: Illegal string offset 'echo' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 15
Warning: Cannot assign an empty string to a string offset in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 15
Warning: Illegal string offset 'content' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 16
Warning: Cannot assign an empty string to a string offset in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 16
Warning: Illegal string offset 'shortcode' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 18
Warning: Illegal string offset 'devs' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 19
For some reason, it's throwing a warning every time it encounters one of the indices in the array. How do I fix this? Thanks!
It looks like the function is expecting an array and is getting a string instead.
You can require an array in the function definition.
function my_function(array $array) { ...
Then if you call it with something other than an array, you'll get a TypeError.
Unfortunately, there will still be a problem somewhere else in your code, where something you thought was an array is actually a string.
Setting up your function like this will generate an error earlier, which is good, because it will make it more obvious where the problem is. If you modify your function to ignore the problem instead, it will probably just create more confusing behavior and potentially different errors.
The usage of the function is_array() in the beginning of your function can give you the insurance, that, if someone pass you something else than an array, the variable is reinitialised as an empty array.
Unsetting or nulling it before doing that is useless, because, as of PHP 5.3, PHP does have a garbage collector mechanism.
/**
* #params array $array
*/
function my_function( $array ) {
if ( ! is_array ( $array ) ) { $array = [] };
/**
* Or, if you don't like the short array notation:
* if ( ! is_array ( $array ) ) { $array = array(); };
*/
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
I am trying to add a value to the end of an array. I am using:
array_push($this->_attributes["class"],$value);
Now I know that the first parameter has to be an array. Upon inspection :
var_dump($this->_attributes["class"]); die(0);
array_push($this->_attributes["class"],$value);
I can see that the value being passed in is indeed an array as it should be. I am not sure how or why I am getting a string being passed. The output of the var_dump look like such:
array (size=0)
empty
Why or how is $this->_attributes["class"] being seen as a string and not an array?
Edit:
If I invert the two lines like so:
array_push($this->_attributes["class"],$value);
var_dump($this->_attributes["class"]); die(0);
The var_dump looks like this:
array (size=1)
0 => string 'btn' (length=3)
This is the expected output. If I remove the var_dump, I get a fatal error on the array_push again.
** Full Class Declaration**
This is enough of the class I able building for this example:
class Tag
{
protected $_attributes = array("class"=>array());
public function setAttribute($attribute,$value)
{
if( $attribute === "class" ) {
$this->setClassAttribute($value);
}
$this->_attributes[$attribute] = $value;
}
public function setClassAttribute($value)
{
if( is_array($value) ) {
foreach ($value as $c) {
$this->setClassAttribute($c);
}
return;
}
// var_dump($this->_attributes["class"]); die(0);
array_push($this->_attributes["class"],$value);
// var_dump($this->_attributes["class"]); die(0);
}
}
To execute it:
$tag = new Tag();
$tag->setAttribute("class","btn");
Your problem is here:
public function setAttribute($attribute,$value)
{
if( $attribute === "class" ) {
$this->setClassAttribute($value);
}
$this->_attributes[$attribute] = $value;
}
setClassAttribute is indeed setting the value to array("btn"). Afterwards, it's being overwritten by the line outside the statement. Try:
public function setAttribute($attribute,$value)
{
if( $attribute === "class" ) {
$this->setClassAttribute($value);
} else {
$this->_attributes[$attribute] = $value;
}
}
REPL:
php > $tag = new Tag();
php > $tag->setAttribute("class","btn");
array(0) {
}
array(1) {
[0]=>
string(3) "btn"
}
php > $tag->setAttribute("class","btn");
string(3) "btn"
Warning: array_push() expects parameter 1 to be array, string given in php shell code on line 21
string(3) "btn"
I have a multidimensional array that I want to access and change one of the values from an integer to a string using a helper function. The code below works but if I remove the if statement which I dont really need it gives me a Undefined index error on the helper code
$stories = multidimensional array in this format
array(1) {
[0]=> array(4)
{
["code"]=> string(1) "2"
["user_name"]=> string(4) "Dave"
["name"]=> string(11) "project one"
["sample_id"]=> string(1) "2"
}
}
to access the array I am using
foreach($stories as $key => $subarray) {
if(array_key_exists('code', $subarray)){
//if($stories[$key]['code'] > 0){
$stories[$key]['code'] = task_priority_text($stories[$key]['code']);
//};
}
}
Commenting the code in this way throws the error while uncommenting gives a clean result.
Here is the helper function which I have used elsewhere and works well
if ( ! function_exists('task_priority_text'))
{
function task_priority_text($priority = FALSE)
{
$options = array('0' => 'Urgent', '1' => 'High', '2' => 'Medium', '3' => 'Low', '4' => 'Minimal');
if($priority !== FALSE)
return $options[$priority];
else
return $options;
}
}
How do I get ride of this if statement?
EDIT
here is the error
A PHP Error was encountered
Severity: Notice
Message: Undefined index: Medium
Filename: helpers/tasks_helper.php
Line Number: 70
line 70 of the helper is
return $options[$priority];
You are looping over each item in the array multiple times. The 'outer' loop runs once for every item in the array, then the 'inner' loop (which someone else pointed out is redundant) runs again for every item in the $subarray variable.
foreach($stories as $key => $subarray) {
foreach($subarray as $subkey => $subsubarray) {
if(array_key_exists('code', $subarray)){
//if($stories[$key]['code'] > 0){
$stories[$key]['code'] = task_priority_text($stories[$key]['code']);
//};
}
}
}
This would be a better way of doing it:
foreach($stories as $story)
{
$story['code'] = task_priority_text($story['code']);
}
I am trying to filter a php array by comparing a field containing a unix date string ("start-unix-date") with the current unix time (simplified in this example as "15"). I want to keep only items where the unix date is in the future (>= than present).
Here is a simplified version of my code - based on this filtering method:
$my_array = array(
array( 'start-date-unix' => '20' ),
array( 'start-date-unix' => '10')
);
$unix_now = 15;
function filter_future($var) {
return (is_array($var) && $var['start-date-unix'] >= $unix_now)
}
$filtered_array = array_filter($my_array, "filter_future");
var_dump($filtered_array);
The expected result:
The $filtered_array should keep only the item with a "start-date-unix" value of 20.
Actual result:
That filter doesn't do anything, every value is accepted. The $filtered_array still contains both items.
What works:
In the filter_future, if I enter the number (15) instead of the $unix_now variable, it works as expected: only the "20" item is kept.
So I suspect that there's an issue with $unix_now being a string vs an integer. I tried many methods of enforcing it being an integer but nothing seems to work :(
$unix_now is out of scope within the callback function:
Use a closure with the use keyword to pass the $unix_now value to the callback
$unix_now = 15;
$filtered_array = array_filter(
$my_array,
function ($var) use ($unix_now) {
return (is_array($var) && $var['start-date-unix'] >= $unix_now)
}
);
$unix_now should be a global variable inside filter_future-function.
$unix_now = 15;
function filter_future($var) {
global $unix_now;
return (is_array($var) && (int) $var['start-date-unix'] >= $unix_now)
}
$filtered_array = array_filter($my_array, "filter_future");
var_dump($filtered_array);
The result:
array(1) { [0]=> array(1) { ["start-date-unix"]=> string(2) "20" } }
$my_array = array(
array( 'start-date-unix' => '20' ),
array( 'start-date-unix' => '10')
);
$unix_now = 15;
foreach ($my_array as $i => $row)
{
if ($row['start-date-unix'] < $unix_now)
{
unset($my_array[$i]);
}
}
Change your function like below:
function filter_future($var) {
$unix_now = 15;
return (is_array($var) && $var['start-date-unix'] >= $unix_now);
}
move $unix_now = 15; inside function filter_future
function filter_future($var) {
$unix_now = 15;
return (is_array($var) && $var['start-date-unix'] >= $unix_now);
}
$unix_now inside your function is not recognize outside variable $unix_now. So put it inside your function is better