• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

Problem with Undefined index in PHP

I am trying to figure out why the following query does not seem to be generating the results I am expecting.  Here is the query:
	$key = array(); $arr1 = array();
	foreach($data['billing_events'] as $billdate) {
		foreach($billdate as $event) {
			$key = array_keys($event);
			$arr1[$key[0]]++; } }   //<-ERROR IS HERE
	$data['arr_bill_dates'] = $arr1;

Open in new window


I am getting an Undefined index error on the line indicated above.  I have verified that the looping structure is working.  When I review the output of this element of the $data array, this is what I get:

    [arr_bill_dates] => Array
        (
            [Jun 10, 2011] => 2
        )

Open in new window


which makes perfect sense to me.  Is there some reason why this:

$arr1[$key[0]]++;

is not being recognized as a valid index for the $data array?  It appears to me that it is being recognized, however, why do I get this error message about Undefined index:


A PHP Error was encountered
Severity: Notice
Message: Undefined index: Jun 10, 2011
Filename: controllers/taxCalcFunction.php
Line Number: 6

Open in new window


Thanks for any ideas you might have to get me pointed in the right direction.
0
DanielAttard
Asked:
DanielAttard
2 Solutions
 
DanielAttardAuthor Commented:
As a follow-up to this question, I just noticed this debug output:

|    ['arr_bill_dates'] => Array (1)
|    (
|    |    ['Jun 10, 2011'] = Integer(1) 2
|    )
|    ['arr_columns'] => Array (1)
|    (
|    |    ['Jun 10, 2011'] => Array (1)
|    |    (
|    |    |    ['IND'] = Integer(1) 2
|    |    )
|    )

Open in new window

Something looks wrong to me about this, although I don't really know how to explain it.  Maybe it will make more sense to someone else.  Thanks.
0
 
Dave BaldwinFixer of ProblemsCommented:
I think you're getting that error because $key[0] is not defined.  You haven't initialized a value for $key[0] before trying to use it.  Defining $key as an array does not initialize any keys or values and the statement $key = array_keys($event); doesn't necessarily do it either.
0
 
Ray PaseurCommented:
The index (key) of an array element can be numeric or it can be associative (character string).  It cannot be both.  If you have an associative array, as you do in both arr_bill_dates and arr_columns and you want to use zero to refer to the "topmost" position of the array, you can use array_values() to reindex the array using numbers for the keys.
http://us2.php.net/manual/en/function.array-values.php

Sidebar note: You might want to read this article.  Your dates will be a lot easier to deal with if you keep the ISO-8601 DATETIME format for the internal representations of date and time values.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Slick812Commented:
greetings  DanielAttard, you have so many array access errors in your code that there is little that has a chance to work.
First, you define the $arr1 as an empty array -
$arr1 = array();

and then later you try and access it, without ever putting any thing in it as -
$arr1[$key[0]]++;

Since I can not really see your array structures and values in $data, I build my own array, based on the things you show for it's structures and values, I ran and tested the code below, how ever I do not see the "Goal" or purpose of your code, seems like it is suppose to just increase ONE integer by one, does not seem useful to me, but maybe I do not see where you need to go with this.

I have 2 html outputs, one with the old vales and after that with the new values, please review you array access methods especially the $arr1  thing.

$data = array('billing_events'=>array('arr_bill_dates'=>array('Jun 10, 2011'=>2,'Jun 11, 2011'=>10)));
foreach($data['billing_events'] as $billdate) {
    $key = array_keys($billdate);
    foreach ($key as $num) $billdate[$num]++;
    break;
    }
// show the OLD values of 2 and 10
foreach ($data['billing_events']['arr_bill_dates'] as $out) echo ',',$out;
echo '<br />';
$data['billing_events']['arr_bill_dates'] = $billdate;// Replace the OLD numbers with the NEW numbers
foreach ($data['billing_events']['arr_bill_dates'] as $out) echo ',',$out;

Open in new window


ask questions if you need more info
0
 
DanielAttardAuthor Commented:
Thanks for the comments everyone.  

Ray - What do you mean by:

the internal representations of date and time values

My dates are stores correctly with proper datatypes in my database, but for the basic looping structure that I have created simply for the purposes of stringing together a series of dates to display as column headings in a table, I would like to keep the array as associative type and not numeric.  

That being said, is there any way that I can leave my arrays as they are somehow avoid the issues raised by DaveBaldwin?  I think you have explained my problem exactly, but how can I overcome the problem?

I don't want to have to change the array or change the error reporting to suppress these types of notifications.  Still wondering if there is a workaround . . .
0
 
DanielAttardAuthor Commented:
Me again guys.  Just wanted to update to say that the workaround that I have been using is to use isset and test the index first before trying to use it.  This is having the desired effect of avoiding the Undefined index notifications, without changing the format of my array data.  Thanks for pointing me in the right direction.
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now