Forum Moderators: coopster

Message Too Old, No Replies

Undefined Index in array

Array Error

         

trebian

3:49 pm on May 17, 2004 (gmt 0)

10+ Year Member



Hello forum:

BACKGROUND:
Create data arrays for graph plots

WHAT WORKS:
The SQL portion, the creation of master array-> from which I chose data array per unique category:


$plot=array();

while ($row=mysql_fetch_assoc($result)) {
$time['time'] =( $row['time']);
$prio['Priority'] = ($row['Priority']);
$valu['total'] =( $row['total']);

//creating arrays
if(!is_array($plot[$prio['Priority']]))
{
$plot[$prio['Priority']] = array();
}
array_push($plot[$prio['Priority']], $valu['total']);
}

DATA DUMP AFTER THIS STEP:
array(4) { ["1 - Started within one hour"]=> array(4) { [0]=> string(2) "59" [1]=> string(2) "72" [2]=> string(2) "45" [3]=> string(2) "19" } ["2 - Started within four hours"]=> array(4) { [0]=> string(3) "118" [1]=> string(2) "77" [2]=> string(3) "107" [3]=> string(2) "58" } ["3 - Started within 24 hours"]=> array(4) { [0]=> string(2) "47" [1]=> string(2) "48" [2]=> string(2) "38" [3]=> string(2) "17" } ["4 - As soon as possible"]=> array(2) { [0]=> string(2) "19" [1]=> string(2) "15" } }

PASSING DATA:
$g1=array_pad($plot['1 - Started within one hour'],4,0);
$g2=array_pad($plot['2 - Started within four hours'],4,0);
$g3=array_pad($plot['3 - Started within 24 hours'],4,0);
$g4=array_pad($plot['4 - As soon as possible'],4,0);

DATA DUMP AFTER THIS:
array(4) { [0]=> string(2) "59" [1]=> string(2) "72" [2]=> string(2) "45" [3]=> string(2) "19" }
array(4) { [0]=> string(3) "118" [1]=> string(2) "77" [2]=> string(3) "107" [3]=> string(2) "58" }

****PROBLEM****
When I pass this $g1, I get the error "UNDEFINED INDEX:'1 - Started within one hour'"

WHAT I NEED:
Can any one tell me if my array construct is right or wrong (I am a newbie myself in PHP)...or am I passing the data values wrong?

TIA

coopster

8:30 pm on May 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I believe your problem lies in your array creation code. If you are using array_push() [php.net] to add one element to the array, you'll find that it is better to use the
$array[] =

syntax because there is no overhead of calling a function. Replace your code here...
//creating arrays 
if(!is_array($plot[$prio['Priority']]))
{
$plot[$prio['Priority']] = array();
}
array_push($plot[$prio['Priority']], $valu['total']);
}

...with this single line:
$plot[$prio['Priority']][] = $valu['total']; 
and see if your problem is cleared up.

trebian

9:13 pm on May 17, 2004 (gmt 0)

10+ Year Member



YOU ARE BEAUTIFUL :)!

Thank you to the nth power. You have no idea, that as a newbie, it took me ages to get to this point...

Thank you, thank you...

Tre