Jump to content

Merging Array Data


chasethemetal

Recommended Posts

Hey all! I am trying to format an array a specific way, and I can't seem to figure out the way to loop the information out so it is formated a specific way. Here's what I'm doing

while ( $data = mysql_fetch_array( $results ) ) {    $arr[] = array( $data['date'], $data['name'], $data['count'] );}

This outputs an array like so:Array( [0] => Array ( [0] => 2012-02-24 [1] => NAME-ONE [2] => 3 ) [1] => Array ( [0] => 2012-02-23 [1] => NAME-ONE [2] => 5 ) [2] => Array ( [0] => 2012-02-23 [1] => NAME-TWO [2] => 2 ))What I am looking for a final result would be Array( [0] => Array ( [0] => Date [1] => NAME-ONE [2] => NAME-TWO ) [1] => Array ( [0] => 2012-02-24 [1] => 3 [2] => 0 ) [2] => Array ( [0] => 2012-02-23 [1] => 5 [2] => 2 ))Notice I need to place a zero if the value doesn't exist... Any pointers on how to make it here would be great! Thanks so much!

Link to comment
Share on other sites

you can use is_null() to check any particular field does not exist or not. assuming your db field supposed to return null if it does not exist

Link to comment
Share on other sites

is_null() can't be used, as I am not storing zero values in the db. The answer for the zero values, is to build an array above the while loop, maybe using the dates as the key, and filling name-one and name-two counts with zeros, then when the while loop fires it will replace the ones that actually have values. I can figure that out, my problems however is more of to HOW can I get the array formatted like my example below, from the example above. Thats the real brain twister here.

Link to comment
Share on other sites

You can predefine the first array values above the while loop using

$arr[] = array( "Date", "NAME-ONE", "NAME-TWO");

Then check the values from data database BEFORE adding to array, if null (you should be able to use is_null() IF this database table field applies a null value when empty) or any other condition you wish to check against.

while ( $data = mysql_fetch_array( $results ){$date = $data['date'];$name = $data['name'];$count = $data['count']; if(is_null($date))	{	$date=0;   }if(is_null($name))   {   $name=0;   }if(is_null($count))	{	$count=0;	}$arr[] = array( $date, $name, $count );}

Sorry! just read your post again! and misunderstood what you wanted, the second example is probably what you require for defining an empty name field with 0 though.

Link to comment
Share on other sites

Thanks! This is almost what I am looking for. However the end result array im looking for would look like $arr[] = array( $date, $count, $count ); I define the names in above, and below am trying to store their relative counts. The predefined array above the while loop is setting labels, And the information that follows, is the date, and counts for each name. Im using the google charts API to graph some things, and the data needs to be formated like this unfortunatly. Thanks, anymore help would be awesome! And again, we can not use the is_null, as I am not storing anything in the database if there is no record. So my database could have a record on 2012-01-30, and then next record being today, 2012-02-25, the end array would need to have all of the dates in between, with zeros. I have accomplished this by creating a daterange function and I have made use of this and is working great where the data needs only 2 columns, date and count. My issue is where I need it formated, as date and count, and count. three columns.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...