Page 1 of 1
foreach() used on an associative array returning first lette
Posted: Mon Jun 21, 2010 1:56 am
by kirstenska
I'm reading and re-reading a post I found (another site) that explains, somewhat, what's happening, but I'm still not understanding why and how to solve it.
I'm trying to access a value via a non-numeric key and isn't working for me. I get the 'first letter' of the array everytime. I guess that my 'temp' variable is a scalar instead of an array... But, I don't understand how to fix this.
My situation: I'm gathering information out of my database (just a few values). Then, adding on one key=>value (seems to be fine). Then, when I try to access this data, no problem if I directly pull it out (for now, I have only one row). But, if I try to use foreach() or while(), I have the single letter issue. I can't understand why I should have to use mysql_fetch_array since I've already done that in my function (stored in a different document). Here's the print_r on my array, and the foreach that doesn't work, as well as the lines that do work (for a single row).
Code: Select all
Array ( [Product] => RS-WW-103 [Size]
=> between 8' x 5' - 12' x 6' [unitCost] => 650.00 [CompleteDesc] => Remodel - Stucco | Wood Window | between 8' x 5' - 12' x 6' [qty] => 5 )
//doesn't work
foreach($productInfoArray as $rowPrint){
echo $rowPrint["qty"]."<br><br>";
echo $rowPrint["CompleteDesc"]."<br><br>";
echo $rowPrint["unitCost"]."<br><br>";
}
//works fine, but only one row
$rowPrint = $productInfoArray;
echo $rowPrint["qty"]."<br><br>";
echo $rowPrint["CompleteDesc"]."<br><br>";
echo $rowPrint["unitCost"]."<br><br>";
Any assistance would be MUCH appreciated!
btw, my resulting printout (i've deleted some <br>'s for clarity) is:
R
R
b
b
b
6
6
6
R
R
R
5
5
5
Re: foreach() used on an associative array returning first l
Posted: Mon Jun 21, 2010 2:10 am
by Phoenixheart
Where is $productInfoArray defined?
If it's a MySQL result, keep in mind that you need to fetch data from it using a mysql_fetch_ function.
Re: foreach() used on an associative array returning first l
Posted: Mon Jun 21, 2010 2:22 am
by kirstenska
Indeed, it is from a MySQL query, but I've already used mysql_fetch_assoc on the original result, then brought the values into this array... code follows:
Code: Select all
$query = "SELECT Product,Size,unitCost,CompleteDesc FROM products WHERE Product like \"$prodChoice\"";
$resultProd = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
while ($row = mysql_fetch_assoc($resultProd)) {
$productInfoArray = $row;
}
$productInfoArray['qty'] = $prodQuantity;
Since I used the while() loop, doesn't that meant that I now have a $productInfoArray that is more than just one row? And, shouldn't I be able to access all that data?
Re: foreach() used on an associative array returning first l
Posted: Mon Jun 21, 2010 9:47 am
by AbraCadaver
You are overwriting your array each time through the loop and it's not the array that you expect. Try this:
Code: Select all
while ($row = mysql_fetch_assoc($resultProd)) {
$productInfoArray[] = $row;
}
This creates an array of your row arrays.
Re: foreach() used on an associative array returning first l
Posted: Mon Jun 21, 2010 10:52 am
by kirstenska
Thank you, AbraCadaver. That did change things (for the better).
Now, there's a really weird thing... now I get an array inside an array:
Code: Select all
Array ( [0] => Array ( [Product] => NC-VW-105 [Size] => Garden window 6' x 5' [unitCost] => 800.00 [CompleteDesc] => New Construction | Vinyl Window | Garden window 6' x 5' ) )
And, if I try to add in my 'qty' value, I get the value put into a second row... Don't know if the array within an array is a problem, but I think it probably will be.
Re: foreach() used on an associative array returning first l
Posted: Mon Jun 21, 2010 11:17 am
by AbraCadaver
kirstenska wrote:Thank you, AbraCadaver. That did change things (for the better).
Now, there's a really weird thing... now I get an array inside an array:
Code: Select all
Array ( [0] => Array ( [Product] => NC-VW-105 [Size] => Garden window 6' x 5' [unitCost] => 800.00 [CompleteDesc] => New Construction | Vinyl Window | Garden window 6' x 5' ) )
And, if I try to add in my 'qty' value, I get the value put into a second row... Don't know if the array within an array is a problem, but I think it probably will be.
If you're going to have multiple rows then you need an array of rows each row containing an array of your columns and data. This should do it:
Code: Select all
while ($row = mysql_fetch_assoc($resultProd)) {
$productInfoArray[] = $row;
$productInfoArray[count($productInfoArray) -1]['qty'] = $prodQuantity;
}
Or you can use a counter:
Code: Select all
$i = 0;
while ($row = mysql_fetch_assoc($resultProd)) {
$productInfoArray[$i] = $row;
$productInfoArray[$i]['qty'] = $prodQuantity;
$i++;
}
Re: foreach() used on an associative array returning first l
Posted: Mon Jun 21, 2010 8:06 pm
by kirstenska
Perfect! Thank you!
Re: foreach() used on an associative array returning first l
Posted: Mon Jun 21, 2010 8:53 pm
by AbraCadaver
Par for the course, Texas teaching silicon valley how to do it.
Re: foreach() used on an associative array returning first l
Posted: Wed Jun 23, 2010 6:48 pm
by kirstenska
You know, I'm still unclear on why I get an array within an array. I'm going to experiment with using array_pop or some-such, but don't understand why that happens in the first place? It didn't seem too concerning at first, but now in another part of my application, it's causing a problem.
Re: foreach() used on an associative array returning first l
Posted: Thu Jun 24, 2010 10:40 am
by AbraCadaver
kirstenska wrote:You know, I'm still unclear on why I get an array within an array. I'm going to experiment with using array_pop or some-such, but don't understand why that happens in the first place? It didn't seem too concerning at first, but now in another part of my application, it's causing a problem.
This is really the way to go for results from multiple database rows. Each row is an array containing the field names as array keys and the data as the array values. So how do you have multiple rows? You build an array of the multiple rows. You need to get your head around this and get your other code working this way.
Re: foreach() used on an associative array returning first l
Posted: Sat Jun 26, 2010 6:33 pm
by kirstenska
AbraCadaver wrote:kirstenska wrote:So how do you have multiple rows? You build an array of the multiple rows. You need to get your head around this and get your other code working this way.
I do understand having multiple rows. However, I don't believe having an array that looks like the following is what is expected:
Code: Select all
Array ( [0] => Array ( [Product] => NC-VW-105 [Size] => Garden window 6' x 5' [unitCost] => 800.00 [CompleteDesc] => New Construction | Vinyl Window | Garden window 6' x 5' ) )
Note the first row is an Array, index 0! I have used an array_pop to get rid of that, but looking at my loop, I don't understand why it happens in the first place.
Re: foreach() used on an associative array returning first l
Posted: Sun Jun 27, 2010 9:24 am
by AbraCadaver
OK, if there's the possibility of returning multiple rows then you need to do it this way. If you're only going to ever return one row then you can do it the way that you posted previously, but you don't foreach over it. Here is what you said:
Code: Select all
//doesn't work
foreach($productInfoArray as $rowPrint){
echo $rowPrint["qty"]."<br><br>";
echo $rowPrint["CompleteDesc"]."<br><br>";
echo $rowPrint["unitCost"]."<br><br>";
}
//works fine, but only one row
$rowPrint = $productInfoArray;
echo $rowPrint["qty"]."<br><br>";
echo $rowPrint["CompleteDesc"]."<br><br>";
echo $rowPrint["unitCost"]."<br><br>";
That is why I solved your problem. What I gave you works for both one row and multiple rows. If the array doesn't work with some of your other code then the other code is incorrect and needs to be changed. If you have to array_pop() or anything else, that means that you are expecting one row and there are better ways to test for/code for one row instead of multiple rows.
Re: foreach() used on an associative array returning first l
Posted: Tue Jun 29, 2010 4:25 am
by kirstenska
I worked through another set of the same sort of data, and FINALLY now understand what you meant by:
If you're going to have multiple rows then you need an array of rows each row containing an array of your columns and data.
Obviously brand new to arrays, and it just wasn't sinking in that each row is it's own array... got it now, and got both instances working well (with single rows and multiple).
Thanks again!