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:


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.
Who is Participating?
Dave BaldwinConnect With a Mentor Fixer 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.
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.
Ray PaseurConnect With a Mentor Commented:
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.

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.

Best regards, ~Ray
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 -

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]++;
// 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
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 . . .
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.
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.