Problem with Undefined index in PHP

Posted on 2012-09-09
Last Modified: 2012-09-11
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.
Question by:DanielAttard

    Author Comment

    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.
    LVL 82

    Accepted Solution

    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.
    LVL 107

    Assisted Solution

    by:Ray Paseur
    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
    LVL 33

    Expert Comment

    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

    Author Comment

    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 . . .

    Author Comment

    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.
    LVL 82

    Expert Comment

    by:Dave Baldwin
    You're welcome, glad to help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now