PHP Undefined offset Message when using "list" function - php

I got this error on my website this morning when I click on post-ad
I have tried looking at the code but don't seem to find anything wrong
if (!function_exists('adforest_extarct_link')) {
function adforest_extarct_link($string) {
$arr = explode('|', $string);
list($url, $title, $target, $rel) = $arr; /* This is line 148 */
$rel = urldecode(adforest_themeGetExplode($rel, ':', '1'));
$url = urldecode(adforest_themeGetExplode($url, ':', '1'));
$title = urldecode(adforest_themeGetExplode($title, ':', '1'));
$target = urldecode(adforest_themeGetExplode($target, ':', '1'));
return array("url" => $url, "title" => $title, "target" => $target, "rel" => $rel);
}
This is the error Message
Undefined offset: 3 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
It actually has 3 lines of error:
Notice: Undefined offset: 1 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
Notice: Undefined offset: 2 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
Notice: Undefined offset: 3 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148

Question is broadly a duplicate of PHP undefined offset from list()
However,
Your list expects at least 4 prameters -- but your $arr array only has 1. So the following three are empty. (remember arrays start at 0). So your $string does not contain a | character for the explode function to work as intended.
Workaround:
Original:
$arr = explode('|', $string);
list($url, $title, $target, $rel) = $arr; /* This is line 148 */
Becomes:
$arr = array_pad(explode('|', $string), 4, null);
list($url, $title, $target, $rel) = $arr;
What this does:
Pads the array out to contain a minimum of 4 values; so that the list values will always be populated, even if they may still be empty.

Related

"Deprecated: parse_str(): Calling parse_str() without the result argument is deprecated in"

I want to run the following command with exec but I am getting an error. So what should I use instead?
php /var/.../../example.php -- 123456789 exampleData1 exampleData2
Error:
Deprecated: parse_str(): Calling parse_str() without the result argument is deprecated in /var/.../../example.php on line X
I tried this:
$argumentsArray = [
'postID' => 123456
'foo' => 'bar'
];
exec(php /var/.../../example.php -- $argumentsArray);
and;
parse_str(parse_url($argv[0], PHP_URL_QUERY),$arguments);
$postID = $arguments['postID'];
Error: Notice: Undefined index: postID in..
You can't substitute an entire array into a string. Use implode() to convert an array into a string with a delimiter between the values.
$argumentsArray = [
'postID' => 123456
'foo' => 'bar'
];
$arguments = implode(' ', $argumentsArray);
exec("php /var/.../../example.php -- {$arguments}");

Wordpress custom plugin error - Warning: Undefined array key "home_team"

i created custom plugin few years ago and now i have in my admin plugin page error Warning: Undefined array key "home_team" ....
Here is my code:
function DBP_insert_data()
{
global $wpdb;
$table_name = $wpdb->prefix . 'funstar_league';
$DBP_home = $_POST['home_team'];
$DBP_guest = $_POST['guest_team'];
$DBP_home_team_url_logo = $_POST['home_team_url_logo'];
$DBP_guest_team_url_logo = $_POST['guest_team_url_logo'];
$DBP_home_team_url_logo = $_POST['home_team_url_logo'];
$DBP_url = $_POST['url'];
$DBP_place = $_POST['place'];
$DBP_time = $_POST['game_time'];
$DBP_date = $_POST['game_date'];
if (isset($_POST['save'])) {
$wpdb->insert(
$table_name,
array(
'home_team' => $DBP_home,
'guest_team' => $DBP_guest,
'home_team_url_logo' => $DBP_home_team_url_logo,
'guest_team_url_logo' => $DBP_guest_team_url_logo,
'url' => $DBP_url,
'place' => $DBP_place,
'game_time' => $DBP_time,
'game_date' => $DBP_date,
),
array(
'%s', // Use string format
'%s',
'%s',
'%s',
'%s',
)
);
}
}
Here is error post:
Warning: Undefined array key "home_team" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 133
Warning: Undefined array key "guest_team" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 134
Warning: Undefined array key "home_team_url_logo" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 135
Warning: Undefined array key "guest_team_url_logo" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 136
Warning: Undefined array key "home_team_url_logo" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 137
Warning: Undefined array key "url" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 138
Warning: Undefined array key "place" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 139
Warning: Undefined array key "game_time" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 140
Warning: Undefined array key "game_date" in /Users/tomasvalach/www/fun_2/wp-content/plugins/extraliga/extraliga_insert_file.php on line 141

Array to string conversion with array_walk in PHP

I am tried to add the same string to every array value.
I tried to use array_walk() like I read it on this answer.
But I get: Notice: Array to string conversion
I have also tried to use array_map(), but I get same error notice.
working code
if ($voice->getValue() === Voice::Passive) {
array_walk($aller_form, function(&$value, $key) { $value .= ' être'; });
$aller_form = [
Mood::Indicatif => [
Tense::Futur_compose => [
Person::FirstPersonSingular => 'vais être',
Person::SecondPersonSingular => 'vas être',
Person::ThirdPersonSingular => 'va être',
Person::FirstPersonPlural => 'allons être',
Person::SecondPersonPlural => 'allez être',
Person::ThirdPersonPlural => 'vont être'
]
]
];
}
return $aller_form[$mood->getValue()][$tense->getValue()][$person->getValue()];
not working code
if ($voice->getValue() === Voice::Passive) {
array_walk($aller_form, function(&$value, $key) { $value .= ' être'; });
}
return $aller_form[$mood->getValue()][$tense->getValue()][$person->getValue()];
EDIT:
The complete error log:
Notice: Array to string conversion in on line 2
Warning: Illegal string offset 'futur_compose' on line 4
Warning: Illegal string offset 'firstPersonSingular' on line 4
(I see 6 times this three error lines for every Person once)

Preserve keys after calling to preg_split

I'm using preg_match for performing a regular expression match, but before using it I declared my array with specific keys as follows:
$record = array ( "title" => '',
"review" => '',
"publisher" => '',
);
and here I call the function
$record = preg_split( "/(\s\|\s)|(\s\|)/", $data, 4 );
If I try to use the array with
echo $record['title'];
I got the following message error:
PHP Notice: Undefined index: title
Is there a way to preserve my keys?

Fatal error:call to undefined method stdClass::xpath()

I have this XML:
<JobReference> <Type>STANDARD</Type> <Title>N° ANCIEN DOSSIER</Title> <Reference/>
And am running this PHP code:
$xmljobreference = simplexml_load_string ($GetJobResult->JobReferences->JobReference->Title );
$referenceanciendossier = $GetJobResult->JobReferences->xpath( "//JobReference[#Title ='N° ANCIEN DOSSIER']/Reference" );
But I am getting this error:
Fatal error:call to undefined method stdClass::xpath() on line 190
If I run var_dump($GetJobResult) I get:
public 'JobReferences' =>
object(stdClass)[149]
public 'JobReference' =>
array (size=7)
0 =>
object(stdClass)[150]
...
1 =>
object(stdClass)[151]
...
2 =>
object(stdClass)[152]
public 'JobSeq' => int 920179
Well, as your var_dump shows you, JobReferences is built up by a JobReference which itself is an array with 7 items.
So assuming that you want to run the xpath function on all the array items, you need to run a foreach loop. Something like this should work:
$array = $GetJobResult->JobReferences->JobReference;
$results = array();
foreach($array as $reference){
array_push($results, $reference->xpath( "//JobReference[#Title ='N° ANCIEN DOSSIER']/Reference" ));
}
var_dump($results);

Categories