Cake PHP: undefined offset error - php

i am newbie with Cake PHP. I am trying to run application that I have downloaded recently but it doesn't work.
My debug.log file says:
2012-07-31 12:31:47 Debug: Notice (8): Undefined offset: 0 in [C:\wamp\www\app\models\vwbrowse.php, line 78]
And my error.log file:
2012-07-31 12:31:47 Warning: Warning (2): array_keys() expects parameter 1 to be array, null given in [C:\wamp\www\app\models\vwbrowse.php, line 78]
And finally this is function that causes problem:
function afterFind($results, $primary)
{
if(!$primary) return $results;
$ret = array();
//we are primary and not part of an associated find
if(!is_array($results)) //find first
{
$tables = array_keys($results);
$record = array();
foreach($tables as $table){
$record = array_merge($record,$result[$table]);
}
$ret['Vwbrowse'] = $record;
}else{ //merge all arrays if separated
$tables = array_keys($results[0]);
foreach($results as $result){
$record['Vwbrowse'] = array();
foreach($tables as $table){
$record['Vwbrowse'] = array_merge($record['Vwbrowse'],$result[$table]);
}
$ret[] = $record;
}
}
return $ret;
}
**This is line 78: $tables = array_keys($results[0]);
**
What is wrong? Thanks in advance for solution.

You're checking to see if $results IS NOT an array, then telling your script to perform an array_keys() on it. Does not compute.
Also, you're trying to access $results[0] without confirmation that the 0th index exists.
Try this first:
if (array_key_exists(0, $results)) { ... }

The undefined index error is stating that the index of the array, in your case [0] is undefined.
You are checking if $results is an array with if (!is_array($results)), but not if it has any data. Try changing it to if (!is_array($results) || (count($results) == 0)).

Related

2 warnings: Cannot use a scalar value as an array and Illegal offset type in isset or empty

Here is the code that I wrote:
class init{
private $time_spent;
public function __construct(){
$this->time_spent = array(); //<- caching variable
}
function get_course_unit_time_spent( $user_id,$course_id,$unit_id ){
if(empty($this->time_spent) || empty($this->time_spent[$course_id]) || empty($this->time_spent[$course_id][$unit_id])){
$this->time_spent[$course_id][$unit_id] = get_user_meta($user_id,'time_spent_'.$course_id.'_'.$unit_id,true);
if( empty($this->time_spent[$course_id][$unit_id]) ){
$this->time_spent[$course_id][$unit_id] = 0;
}
}
return $this->time_spent[$course_id][$unit_id];
}
function get_course_time_spent( $user_id,$course_id ){
if( empty($this->time_spent[$course_id]) ){
if(!bp_course_is_member($course_id,$user_id))
return 0;
$time_spent = 0;
$this->time_spent[$course_id] = array();
$course_curriculum = bp_course_get_curriculum( $course_id );
foreach ($course_curriculum as $key => $unit_id) {
if( is_numeric($unit_id) ){
$time_spent += $this->get_course_unit_time_spent( $user_id,$course_id,$unit_id );
}
}
$this->time_spent[$course_id] = $time_spent;
}
return $this->time_spent[$course_id];
}
}
There are lots of code but the issue is with the first function I wrote above, in two different places I am getting the above to warnings.
1) Warning: Cannot use a scalar value as an array on line 10 and 12 (in the above code).
2) Warning: Illegal offset type in isset or empty on line 8 (in the above code).
3) Warning: Illegal offset type on line 10, 11 and 12 (in the above code).
The second function is not throwing any error but whenever I use the 1st function or the second everytime the warning comes from the 1st function. I am not sure what is it, can someone help to correct it ?
UPDATE: Updated the code and now the first warning is gone but still getting the illegal ofset error.
Your calling get_course_time_spent(), which creates an array for
$this->time_spent[$course_id][$unit_id] in
get_course_unit_time_spent()
Then your setting $this->time_spent[$course_id] as an int once the foreach loop has finished.
Then on the next call to get_course_unit_time_spent your checking
as if its an array empty($this->time_spent[$course_id][$unit_id])

Variable is not an array PHP

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

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

Filtering a Multidimensional Array based of another array

I'm creating a PHP class that manipulates csv files.
As part of the class I have a function that allows the data to be filtered showOnlyWhere. However I get this error Invalid argument supplied for foreach() on line 331 (the line with the foreach statement). I tried adding global $arr; but that didn't work. How would i fix it?
$this -> rows is a multi-dimensional array that contains all the csv data.
$arr is in the format:
$key=>$val array(
$key = Column Name
$val = value that column should contain
)
Below is the showOnlyWhere function
function showOnlyWhere($arr)
{
if($this->showOnlyWhere == true){
$rows = $this->filteredRows;
}
else{
$rows = $this->rows;
}
$filter = function ($item){
global $arr; // didn't work
foreach($arr as $chkCol => $chkVal){
if ($item[$arr[$chkCol]] != $chkVal ){
return false;
break(3);
}
}
return true;
};
$this->filteredRows = array_filter($rows,$filter);
$this->showOnlyWhere = true;
}
I think the error might have something to do with the Anonymous function - but I'm not really sure.
instead of using global $arr you can make $arr available to the anonymous function via use
$filter = function ($item) use ($arr) {
//global $arr; // didn't work
foreach($arr as $chkCol => $chkVal){
if ($item[$arr[$chkCol]] != $chkVal ){
return false;
}
}
return true;
};
Also, I noticed that you are assigning $rows = $this->filteredRows; before you populate $this->filteredRows. I'm not sure if that's intentional?
Format for your $arr is wrong.
this is wrong:
$key=>$val array(
$key = Column Name
$val = value that column should contain
)
You cannot supply class objects to foreach, it should be a valid array.
It should be like this:
$arr=array(
$key => 'Column Name',
$val = 'value that column should contain'
);
So first convert your object to a valid array.

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