Populating Associate Arrays in PHP


 
Thread Tools Search this Thread
Top Forums Programming Populating Associate Arrays in PHP
# 1  
Old 09-30-2011
Populating Associate Arrays in PHP

I'm not very good at associative arrays; and working on this PHP code has got me a bit stumped. My goal is to populate a (multidimensional) associative array in a PHP while look after a MySQL query. The code fragment looks like this:

PHP Code:
while($campaign_row mysql_fetch_array($campaigninfo))
{
   
$total 0;
   
$bannerid_row mysql_query("SELECT bannerid FROM ox_banners WHERE campaignid=".$campaign_row['campaignid']) or die(mysql_error());
   while(
$row mysql_fetch_array($bannerid_row))
    {

     
$ad_impressions mysql_query("SELECT impressions FROM ox_data_summary_ad_hourly WHERE ad_id=".$row['bannerid']) or die(mysql_error());

     while(
$ad_imps_row mysql_fetch_array($ad_impressions))
     {
       
$total += $ad_imps_row['impressions'];
     }
    }

$remaining $campaign_row['views'] - $total ;

$info = array($campaign_row['campaignid'] => array("name" => $campaign_row['campaignname'],"booked" => (int)$campaign_row['views'], "served" => $total "remaining" => $remaining,"weight" => (int)$campaign_row['weight'] ));

print_r($info) ; 
Fills like this ( print_r($info) ; ):

Quote:
Array ( [123] => Array ( [name] => NAME1 [booked] => 110000 [served] => 103027 [remaining] => 6973 [weight] => 3 ) )

Array ( [118] => Array ( [name] => NAME2 [booked] => 29000 [served] => 26622 [remaining] => 2378 [weight] => 1 ) )

Array ( [129] => Array ( [name] => NAME3 [booked] => 110000 [served] => 104667 [remaining] => 5333 [weight] => 2 ) )
But is not creating the expected array indexes based on:

PHP Code:
$campaign_row['campaignid'
Because when I dump the array out of the while loop, I get the last entry (NAME1) only:

Quote:
Array ( [129] => Array ( [name] => NAME3 [booked] => 110000 [served] => 104667 [remaining] => 5333 [weight] => 2 ) )

Array ( [129] => Array ( [name] => NAME3 [booked] => 110000 [served] => 104667 [remaining] => 5333 [weight] => 2 ) )

Array ( [129] => Array ( [name] => NAME3 [booked] => 110000 [served] => 104667 [remaining] => 5333 [weight] => 2 ) )
... which has got me completely confused and searching the net for examples or tutorials of how to properly populate this array based on the campaignid index has turned up blanks.

Anyone know how to do this?
# 2  
Old 09-30-2011
The result must be full of references, which act like variables but copy around like pointers, leading to values which can mysteriously change later like you've observed.

Converting them to a string like "$THIS['val']" should prevent this I hope.

I suspect you might be able to convert those two queries into a single one with judicious use of SUM() and GROUP BY but that'll need thinking on.

---------- Post updated at 09:28 AM ---------- Previous update was at 09:19 AM ----------

Perhaps
PHP Code:
"SELECT bannerid, SUM(impressions) FROM ox_banners
          JOIN ox_data_summary_ad_hourly ON(ad_id=bannerid)
          WHERE ox_banners.campaignid=
$campaign_row['campaignid']
          GROUP BY(ox_banners.bannerid) 
If any of those columns are ambiguous, i.e. exist in both tables you'll need to specify table.varname for them.
# 3  
Old 09-30-2011
I tried a number of things, and when I do this:

Code:
$info = array("$campaign_row['campaignid']" => array("name" => $campaign_row['campaignname'],"booked" => (int)$campaign_row['views'], "served" => $total , "remaining" => $remaining,"weight" => (int)$campaign_row['weight'] ));

The result is a PHP error:

Quote:
[30-Sep-2011 22:06:14] PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /blah/blah/index.php(36) : eval()'d code on line 48
# 4  
Old 09-30-2011
My mistake. You don't use single quotes inside double quotes for associative array keys, you leave the string unquoted.

PHP Code:
<?php $VAR_NAME=array("z" => "a""q" => "b");
print 
"$VAR_NAME[q]\n"?>
EOF

b
---------- Post updated at 04:30 PM ---------- Previous update was at 04:27 PM ----------

You'd want to quote everything you use from $campaign_row, by the way, not just the array key. The one you convert to (int) should be fine though.
# 5  
Old 10-01-2011
So, you think this will work?

Code:
$info = array("$campaign_row[campaignid]" => array("name" => $campaign_row['campaignname'],"booked" => (int)$campaign_row['views'], "served" => $total , "remaining" => $remaining,"weight" => (int)$campaign_row['weight'] ));

Just remove the single quotes from the first / main key?
# 6  
Old 10-01-2011
I tried removing the single quotes; no error, but the results were the same as the original post.

These complex associative arrays are hard Smilie
# 7  
Old 10-01-2011
I am thinking to give up on an associative array of multi-dimension for this code and move to numeric arrays; after searching the net fairly exhaustively, I've not found a similar example of a multidimensional associative array (that works).

Edit: This tutorial looks promising.

After looking at the tutorial, the problem may be as simple as this:

Code:
$info[] = blah blah blah;

As I have missed, it seems, that to update an array in PHP we need to add the "[]", so the tutorial says... so I'll try to just initialize the array and then update each time using the "[]" construct ... I hope it is that simple !

Last edited by Neo; 10-01-2011 at 07:30 AM.. Reason: Added reference to link
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

2. Programming

populating a JList

Hi, I have to create a JList and the items I need to display are store in HashMap table. What would be the easiest way to populate this JList. Basically the items I want to display/show in the JList are the key values of the HashMap. Thanks in advance for any suggestions. (0 Replies)
Discussion started by: arizah
0 Replies

3. Shell Programming and Scripting

PHP Compare 2 Arrays find difference & case insensitive

Hi, I need an elegant solotion in php. I need to compare 2 arrays (array1 & array2), to find all instances of array 2 which is not in array1. I don't want to see any instances of array1 wich is not in array2 (here "the") Array1: This, is, the, data, of, array1 Array2: this, is, data, Of,... (2 Replies)
Discussion started by: lowmaster
2 Replies

4. Shell Programming and Scripting

Populating an Array

Guys, I need to iterate populate an array while going over files in directory. Can someone please tell me syntax I tried this but it isn't working ==> for F in `ls -p "${directory1}" | grep -v "\/"` do cd "${directory2}" cmp "${directory2}"/"${F}" "${directory1}"/"${F}" ... (2 Replies)
Discussion started by: Veenak15
2 Replies

5. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

6. Shell Programming and Scripting

PHP arrays as array elements

PHP question...I posted this on the Web Development forum, but maybe this is a better place! I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person... (3 Replies)
Discussion started by: JerryHone
3 Replies

7. Shell Programming and Scripting

populating array using awk

Hi. I have a file with the following structer: DB DISK LOCATION SIZE ============================================ PROD DATA_01 /dev/dm-23 10 PROD DATA_02 /dev/dm-24 10 PROD DATA_03 /dev/dm-25 10 DEV DATA_04 /dev/dm-26 10 DEV DATA_05 ... (1 Reply)
Discussion started by: yoavbe
1 Replies

8. Shell Programming and Scripting

Awk help with populating variable

Hi, I'm looking for help trying to parse a data stream. Any help would be greatly appreciated. My awk statement is awk '/Aug/{a=$2}/vol/{print a, host, $1, $2, $3, $4, $5}' out.txt Sample Data Stream "out.txt" ----------------------------- # Aug 3 00:00:00 2008 ===== DF =====... (3 Replies)
Discussion started by: jmd2004
3 Replies

9. Shell Programming and Scripting

associate array problems in awk

hi, i have 3 fields in a file and linked them through 2 associative arrays.. the problem is one of the associative array is working while the other is not.. the code part is: awk ' BEGIN { FS="|" rc = getline < "ICX_RULES" while ( rc == 1 ) { rule_id=$1 rule_parameter=$2... (2 Replies)
Discussion started by: aami_sayan
2 Replies

10. UNIX for Dummies Questions & Answers

Problems associate with upgrade

Dear all, We are planning on upgrading our current HP unix 64 bites from version 11.0 to 11.11. I would like to find out all the possible problems that we will encounter especially on the application side. Can anyone provide me with list(s) or link(s) where I can find out more information? ... (0 Replies)
Discussion started by: wujee
0 Replies
Login or Register to Ask a Question