Page 1 of 1

adding elements to the correct place in an array

Posted: Fri Nov 19, 2010 6:06 am
by IGGt
I have a problem with a multi level array.

Basically what I have so far is:

Code: Select all

//list of all database connections
$connections_array[6] = array('server' => '1.2.3.4',
                             'user' => $u,
                             'password' => $p,
                             'database' => 'MF1',
                             'sendmail' => '0',
                             'repeat' => '0',
                             'dbID' => '07',
                             'default' => 'test'
                                           );

//the database required here
$mfA_array = 	 $connections_array[6];

//List of MyQSL queries
$q1 = "SELECT COUNT(*) AS 'count' FROM mf1 WHERE LEFT(`starttime`,10) = CURDATE() AND `customerid` =";
$q2 = ...
$q3 = ...

//mf query_array
$mfquery_array = array();
$mfquery_array[] = array(	'query1' => $q1
					//'query2' => $q2,
					//'query3' => $q3 
							);

//list of Customers
$Aarray[] = array(	'name' => "Cust1",
					'id' => "5664");
$Aarray[] = array(	'name' => "Cust2",
					'id' => "5585");
$Aarray[] = array(	'name' => "Cust3",
					'id' => "5584");
$Aarray[] = array(	'name' => "Cust4",
					'id' => "5665");
$Aarray[] = array(	'name' => "Cust5",
					'id' => "5652");
$Aarray[] = array(	'name' => "Cust6",
					'id' => "5654");

//Run the queries and put the result into an array
	$con = mysql_connect($mfA_array['server'], $mfA_array['user'], $mfA_array['password']);
		mysql_select_db($mfA_array['default'], $con);
       
        	foreach($mfquery_array as $query) {

	        	foreach($Aarray as $cust) {
		        	
// QUERY 1		        	
		        	$fquery = $query['query1'].$cust['id'];
	       				$Mfres1 = mysql_query($fquery);
        	    	
        				if ( !$Mfres1 ) { 	$error = mysql_error();
        					print $error;
    					}
    					
    					while($Mfrow1 = mysql_fetch_assoc($Mfres1)) {
	                		$totalJobs[$db['database']] = $Mfrow1;
	                		$Aarray[] = array('count' => $Mfrow1['count']);
	                		
	               						}
               					       							
// QUERY 2        							
		
// QUERY 3  
               						          
        						}

        						print_r ($Aarray);	
    						}        							        							
	mysql_close($con);

The idea is that
1. I connect to the database
2. I get a list of queries to run against that database
3. I get a list of customer ID's to insert into the queries
4. I run each query with the customer ID added, and then add the result to the $Aarray

So what I want to end up with is something like:
print_r ($Aarray)
Array (
[0] => Array (
[name] => Cust1
[id] => 5664
[count] => 100
)
.....

What I am currently getting is
Array (
[0] => Array (
[name] => Cust1
[id] => 5664
)
. . . . .
[6] => Array (
[count] => 100
)
[7] => Array (
[count] => 120
)
. . . . .

As you can see the count values are being added to the end of the array as a separate element, instead of being associated with the relevant customer.

How can I get the value to associate with the relevant customer?

Re: adding elements to the correct place in an array

Posted: Fri Nov 19, 2010 6:22 am
by IGGt
typical, no sooner than I post, and I find the answer

Code: Select all

//Run the queries and put the result into an array
        $con = mysql_connect($mfA_array['server'], $mfA_array['user'], $mfA_array['password']);
                mysql_select_db($mfA_array['default'], $con);
       
                foreach($mfquery_array as $query) {

                        for($a = 0; $a <sizeof($Aarray); $a++) { // LINE ADDED
                        //foreach($Aarray as $cust) {   // LINE REMOVED
                               
// QUERY 1                             
                                $fquery = $query['query1'].$Aarray[$a]['id'];     // LINE CHANGED
                                        $Mfres1 = mysql_query($fquery);
                       
                                        if ( !$Mfres1 ) {       $error = mysql_error();
                                                print $error;
                                        }
                                       
                                        while($Mfrow1 = mysql_fetch_assoc($Mfres1)) {
                                        $totalJobs[$db['database']] = $Mfrow1;
                                        //$Aarray[] = array('count' => $Mfrow1['count']);        // LINE REMOVED
                                        $Aarray[$a]['count'] = $Mfrow1['count'];                     // LINE ADDED
                                       
                                                                }
                                                                                                       
// QUERY 2                                                             
               
// QUERY 3  
                                                                 
                                                        }

                                                        print_r ($Aarray);     
                                                }                                                                                                                              
        mysql_close($con);

Re: adding elements to the correct place in an array

Posted: Fri Nov 19, 2010 6:23 am
by internet-solution
If you assign an element to the array without specifying the index ($Aarray[] =array(..) etc.) , it will add a new element.


Replace the following code

Code: Select all

$Aarray[] = array('count' => $Mfrow1['count']);

with

Code: Select all

$cust['count'] = $Mfrow1['count'];
As you are using foreach to iterate through $Array, $cust will contain the array element for the relevant customer and the above code will add a new element "count" to the customer's array.

Re: adding elements to the correct place in an array

Posted: Fri Nov 19, 2010 8:19 am
by IGGt
Cheers,

the only problem I have with this is that when I do

print_r ($cust)
on the next page, after I have completed all the queries, I only have one entry (the last customer)