I have a fatal error claiming that I have a string when I thought I had an array :
Fatal error: [] operator not supported for strings in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 566
and also the following warning :
Warning: in_array() expects parameter 2 to be array, string given in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 579
I am guessing that I have either a syntax problem somewhere? I thought I had initailized the variables but maybe I missed one? I would appreciate some more experienced eyes having a look.
function forum_subscribe_member_player()
{
global $user_ID;
$players= get_users();
foreach($players as $player)
{
$user_info = get_userdata($player->ID);
$playeremail = $user_info->user_email;
if(!empty($playeremail) && user_can( $player-> ID, 'contributor'))
{
$list = get_option('mf_forum_subscribers_1', array());
if( is_player_subscribed($player->ID)) //remove player if already exists (user clicked unsubscribe)
{
$key = array_search($playeremail, $list);
unset($list[$key]);
}
else
$list[] = $playeremail;
update_option('mf_forum_subscribers_1', $list);
}
}
}
function is_player_subscribed($user_ID)
{
if($user_ID)
{
$useremail = get_userdata($user_ID, 'user_email');
$list = get_option("mf_forum_subscribers_1", array());
if(!empty($list) && in_array($useremail, $list))
{
return true;
}
return false;
}
}
function call_forum_subscribe_member_player()
{
forum_subscribe_member_player();
}
line 566 is $list[] = $playeremail; line 579 is if(!empty($list) && in_array($useremail, $list))
well, you can type-cast the $list to an array to make sure it's always an array even if the initial value is null/empty/etc (basically any type except array):
$list = (array)get_option('mf_forum_subscribers_1', array());
Related
i have string of html which i want to explode by <br>, but sometimes it can be inside of other tags.
if (in_array($param, $customOrdering, true) && $value) {
$ordering = array_search($param, $customOrdering, true);
$segments[$ordering] = [];
if (in_array($param, $explodeParams, true)) {
$values = explode('<br>', $value);
foreach ($values as $v) {
$testing_value = $v;
if (!empty(trim(strip_tags($testing_value)))) {
array_push($segments[$ordering], $this->createSegmentFromParam($param, $v));
138 } elseif (!empty(trim($v)) && $segments[$ordering]) {
139 end($segments[$ordering])->value_raw .= strip_tags($v, $this->allowedTags);
}
}
} else {
array_push($segments[$ordering], $this->createSegmentFromParam($param, $value));
}
}
declaration of createSegmentFormParam
#return ArrayHash
private function createSegmentFromParam($param, $value)
and i have this output from phpStan
138 Right side of && is always false.
139 Cannot access property $value_raw on false.
any ideas how to pass on ? code working well
There is still an open issue for similar behaviour or PHP Stan - False negative with array_push on property
Try not using array_push and replace it on line 138 with the following:
$segments[$ordering][] = $this->createSegmentFromParam($param, $v);
Apart from this in the beginning of your code array_search may return FALSE you do want to check this before going further down the code...
I get some data externally but gets this error because the variable is "empty":
Undefined property: stdClass::$summary in /Applications/MAMP/htdocs/jobportal/functions.php on line 68
I have tried to build a function to help me:
$summary = convert_empty($user->summary);
function convert_empty($data) {
if(isset($data)) {
return $data;
} else {
return ".";
}
}
But the error is still there. I have tried isset, empty, and defined. I think I miss another point here - since none of it is working.
That means that the object $user doesn't have a summary member variable defined.
$summary = isset($user->summary) ? convert_empty($user->summary) : NULL;
Or
$summary = isset($user->summary) ? convert_empty(isset($user->summary) ? $user->summary : NULL);
Now you won't see the warning and $summary will be set to NULL, assuming that you're expecting $summary to be NULL in this situation in which $user->summary is undefined.
The second one allows your convert_empty to figure it out.
The issue is not in your function, but how you call it. The error is that you're trying to access ->summary but doesn't exists. You could use something like this:
$summary = convert_empty($user, 'summary');
function convert_empty($data, $key) {
if (isset($data->$key))
return $data->$key;
return ".";
}
Note that you should also test if $data is an object too.
if (is_object($data) && isset($data->$key)) { ... }
Or, without a function using conditional ternary operator :
$summary = isset($user->summary) ? $user->summary : '.';
EDIT for a deeper use :
convert_empty($user, 'positions', 'values', $i, 'title');
function convert_empty($obj) {
$error = '.';
$args = func_get_args();
array_shift($args); // remove $obj
$ref = $obj ;
foreach ($args as $arg) {
if (is_array($ref)) {
if (!isset($ref[$arg])) return $error ;
$ref = $ref[$arg] ;
}
elseif (is_object($ref)) {
if (!isset($ref->$arg)) return $error ;
$ref = $ref->$arg ;
}
}
return $ref ;
}
when i try use in_array function in php for the second time for same array variable i got the following error saying:
in_array() expects parameter 2 to be array, string given in
when i wrap the function in condition is_array, it returns false, i already print the variable using print_r and its showing array structure, here's the code:
$chosenCour = array();
$chosenServ = array();
foreach ($preferences as $preference) {
if(!in_array($preference['courier'],$chosenCour)){
$chosenCour[] = $preference['courier'];
}
if(!in_array($preference['courier_service'],$chosenServ)){
$chosenServ[]= $preference['courier_service'];
}
}
foreach ($couriers as $courier) {
$courCond = false;
if(is_array($chosenCour)){
if(in_array($courier['courier_id'],$chosenCour)){
$courCond = true;
}
}
}
Check the additional condition for non empty array
foreach ($couriers as $courier) {
$courCond = false;
if(is_array($chosenCour) && count($chosenCour) > 0){
if(in_array($courier['courier_id'],$chosenCour)){
$courCond = true;
}
}
}
Wrote this function to clean up array and LEAVE 0, null & false as valid values.
function sanitize_array($array = array()) {
if (is_array($array)) {
$filtered_array = array_filter($array, create_function('$a', 'return trim($a)!=="";'));
$filtered_and_trimmed_array = array();
foreach ($filtered_array as $k => $v) {
if (!is_array($v) && !is_object($v)) {
$filtered_and_trimmed_array[$k] = htmlspecialchars(trim($v));
}
if (is_array($v)) {
$filtered_and_trimmed_array[$k] = $this->sanitize_array($v);
}
}
return $filtered_and_trimmed_array;
} else {
return false;
}
}
It's giving me the following error:
g: trim() expects parameter 1 to be string, array given in /home/imgimportsinc/public_html/img_scripts_library/img_functions.php(328) : runtime-created function on line 1
Warning: trim() expects parameter 1 to be string, array given in /home/imgimportsinc/public_html/img_scripts_library/img_functions.php(328) : runtime-created function on line 1
You just need to check that the value to be evaluated is a string before trimming it.
$filtered_array = array_filter($array, function($v) {
return !is_string($v) || (is_string($v) && trim($v) !== "");
});
I have a wordpress function that calls an API via Zebra_cURL and then uses json_decode to render the data appropriately. Yet there is erratic behavior and the following two errors are occurring: Trying to get property of non-object in [template file] on line 42 and through lines 50.
The lines in question are below:
//Course display callback function
function display_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$title = $result->body[0]->{'Title'};
$term = $result->body[0]->{'Term'};
$meetings = $result->body[0]->{'Meetings'};
$status = $result->body[0]->{'Status'};
$course_number = $result->body[0]->{'OfferingName'};
$clean_course_number = preg_replace('/[^A-Za-z0-9\-]/', '', $course_number);
$credits = $result->body[0]->{'Credits'};
$instructor = $result->body[0]->{'InstructorsFullName'};
$description = $result->body[0]->{'SectionDetails'}[0]->{'Description'};
}
And then later I get an error Invalid argument supplied for foreach() in [template file] on line 64 which is:
//Call callback function
function parse_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$course_data = array();
foreach($result->body as $course) {
[code]
}
[more code]
}
What exactly am I doing wrong here to cause these errors?
Here's a link to the full template file. Apologies for the vast code dump but I'm a bit in over my head with this.
You must always verify the output of functions/methods if the result if "as expected"
In your case, you did not check what json_decode() has returned
function parse_courses($result) {
$result = json_decode(html_entity_decode($result->body));
if ((!is_array ($result) && !is_object($result)) ||
(is_array($result) || count($result) == 0) ||
(json_last_error() != JSON_ERROR_NONE)) { // only for PHP >= 5.3.0
// log the error or warning here ...
// $input = $result;
// $output = print_r ($result, TRUE);
// Only for PHP >= 5.3.0
// json_last_error();
// json_last_error_msg();
return -1;
}
$result->body = $result;
$course_data = array();
foreach($result->body as $course) {
[code]
}
[more code]
}