Plugin Error - array_merge(): Argument #2 is not an array - php

I am building my first wordpress plugin and the error log is throwing up the following errors.
[06-Jul-2014 20:07:21 UTC] PHP Warning: array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 49
[06-Jul-2014 20:07:21 UTC] PHP Warning: array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 50
[06-Jul-2014 20:07:21 UTC] PHP Warning: end() expects parameter 1 to be array, null given in \wp-content\plugins\test-plugin\screen.php on line 323
[06-Jul-2014 20:07:21 UTC] PHP Warning: Invalid argument supplied for foreach() in \wp-content\plugins\test-plugin\screen.php on line 324
For the errors on line 49 & 50 the code is:
function register_settings() {
global $menu;
global $submenu;
$this->menus = array_merge(array(), $menu);
$this->submenus = array_merge(array(), $submenu);
$this->settings = get_option( $this->settings_name );
register_setting( 'admin-theme', $this->settings_name );
}
For the errors on lines 323 & 324 the code is:
function admin_menu() {
global $menu;
global $submenu;
// update menu
end( $menu );
foreach ($menu as $k=>&$v){
$id = explode(' <span', $v[0]);
$slug = 'menu_'.strtolower( str_replace( ' ','_',$id[0] ) );
$slug_hide = $slug.'_hide';
if($id[0] != NULL && $this->get_setting($slug) !== NULL){
$v[0] = $this->get_setting($slug). ( isset($id[1]) ? ' <span '.$id[1] : '' );
}
if( $this->get_setting($slug_hide) ){
unset($menu[$k]);
}
// update the submenu
if( isset($submenu[$v[2]]) ){
foreach ($submenu[$v[2]] as $key=>&$val){
$id = explode(' <span', $val[0]);
$slug_sub = $slug.'_'.strtolower( str_replace( ' ','_',$id[0] ) );
$slug_sub_hide = $slug_sub.'_hide';
if($id[0] != NULL && $this->get_setting($slug_sub) !== NULL){
$val[0] = $this->get_setting($slug_sub). ( isset($id[1]) ? ' <span '.$id[1] : '' );
}
if( $this->get_setting($slug_sub_hide) ){
unset( $submenu[$v[2]][$key] );
}
}
}
}
}
I can't seem to figure it out so if anyonecanhelp or point me in the right direction it would be greatly appreciated.

first you need to check your both variables is array
with is_array() like if(is_array($menu))
if not use type cast to convert variable in array
if(is_array($menu))
$this->menus = array_merge(array(), $menu);
else
$this->menus = array_merge(array(), (array)$menu);
if(is_array($submenu))
$this->submenus = array_merge(array(), $submenu);
else
$this->submenus = array_merge(array(), (array)$submenu);

Related

PHP Error - Warning: count(): Parameter must be an array or an object that implements Countable

The full error is:
"Warning: count(): Parameter must be an array or an object that implements Countable in /app/public/wp-content/themes/site/inc/lists_posts.php on line 54"
It's saying line 54 which is where the first if statement starts.. Any idea how I can fix this?
<?php foreach($projects as $key => $project){
$display = true;
$terms = get_the_terms($project->ID,"diadem_project_category");
if(count($terms) == 1){
// function located in function.php
$display = check_project_display($terms[0]->term_id);
}
if($display){
$permalink = get_permalink($project->ID);
$project_caption = get_field('project_caption',$project->ID);
$type = get_field('type',$project->ID);
$subscript_number = get_field('subscript_number',$project->ID);
$download_pdf = get_field('download_pdf',$project->ID);
$thumbnail = get_the_post_thumbnail_url($project->ID);
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id($project->ID), 'medium');
$categories = wp_get_post_terms($project->ID, $category_name, array("fields" => "all"));
$target ="";
$slug = "";
if($categories){
foreach($categories as $cat){
$slug .= $cat->slug." ";
if($category_name == "diadem_insight_category"){
$project_caption = $cat->name;
}
}
}
$project->post_title = str_replace("'","’",$project->post_title);
?>
Sometimes while getting data, some errors occurs which return false instead of result set. So before checking data length, we should compare output type too.
if($terms !== false && count($terms) == 1){
// function located in function.php
$display = check_project_display($terms[0]->term_id);
}
And also I think you might get n terms not only one. In that case
if($terms !== false && count($terms) >= 1){
// function located in function.php
$display = check_project_display($terms[0]->term_id);
}
First check the datatype of variable $terms using var_dump($terms) or echo gettype($terms).It must be an array or object then only warning will not occurs.The count() method which you have used expects paramter datatype should be array or object.

What is wrong with the lines? (php)

I get these error from nowhere... What is wrong?
Warning: Division by zero in
/home//public_html/wp-content/plugins//includes/classes/controller/SearchPageController.php
on line 60
Warning: Invalid argument supplied for foreach() in
/home/vineanimals/public_html/wp-content/plugins/*/includes/classes/controller/ImportPageController.php
on line 70
Warning: array_merge(): Argument #1 is not an array in
/home//public_html/wp-content/plugins//includes/classes/controller/AImportPageController.php
on line 75
line codes:
line 60: $last = ceil($load_products_result['total'] / $load_products_result['per_page']);
line 70-75:
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}
A) Check your SQL query or Array feed
B) Then change
$last = ceil($load_products_result['total'] / $load_products_result['per_page']);
to:
$last = ( isset($load_products_result['per_page']) ? ceil($load_products_result['total'] / $load_products_result['per_page']) : 0)
C) and change the foreach as below:
if (is_array($product['sku_products']['variations'])){
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
if (isset($product['description']) && is_array($product['all_images']) && is_array($this->woocommerce_model->get_images_from_description($product['description'])) )
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}

JSON Property of non-object & Invalid argument supplied for foreach

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]
}

How to solve Fatal error in php

While running the codes on local host it is showing an error of--
( ! ) Fatal error: Call to a member function find() on a non-object in
C:\wamp\www\crawler_based_search_engine\ajax_requests.php on line 130
Call Stack
# Time Memory Function Location
1 0.0151 162928 {main}( ) ..\ajax_requests.php:0
the codes from line 130 and beyond is as follows
$html = file_get_html($file_name);
// Find all links
foreach($html->find('a') as $element) //--this is line no 130
{
$url = $element->href;
$index = strpos($url,'q=https://');
if($index > 0)
{
$index2 = strpos($url,'webcache');
if($index2 === false)
{
$index2 = strpos($url,'sa=U');
$url = substr($element->href,$index+2,$index2-$index-3);
echo "<a href='$url'>$url</a>";
echo '<hr/>';
}
}
}
because of this error my program is not fetching the links from the google

Fatal error when value empty

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());

Categories