foreach() used on an associative array returning first lette

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kirstenska
Forum Newbie
Posts: 7
Joined: Mon Jun 21, 2010 1:50 am
Location: Santa Clara, CA (silicon valley, baybee!)

foreach() used on an associative array returning first lette

Post 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
Last edited by kirstenska on Mon Jun 21, 2010 2:31 am, edited 1 time in total.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: foreach() used on an associative array returning first l

Post 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.
kirstenska
Forum Newbie
Posts: 7
Joined: Mon Jun 21, 2010 1:50 am
Location: Santa Clara, CA (silicon valley, baybee!)

Re: foreach() used on an associative array returning first l

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach() used on an associative array returning first l

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kirstenska
Forum Newbie
Posts: 7
Joined: Mon Jun 21, 2010 1:50 am
Location: Santa Clara, CA (silicon valley, baybee!)

Re: foreach() used on an associative array returning first l

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach() used on an associative array returning first l

Post 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++;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kirstenska
Forum Newbie
Posts: 7
Joined: Mon Jun 21, 2010 1:50 am
Location: Santa Clara, CA (silicon valley, baybee!)

Re: foreach() used on an associative array returning first l

Post by kirstenska »

Perfect! Thank you!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach() used on an associative array returning first l

Post by AbraCadaver »

Par for the course, Texas teaching silicon valley how to do it.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kirstenska
Forum Newbie
Posts: 7
Joined: Mon Jun 21, 2010 1:50 am
Location: Santa Clara, CA (silicon valley, baybee!)

Re: foreach() used on an associative array returning first l

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach() used on an associative array returning first l

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kirstenska
Forum Newbie
Posts: 7
Joined: Mon Jun 21, 2010 1:50 am
Location: Santa Clara, CA (silicon valley, baybee!)

Re: foreach() used on an associative array returning first l

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach() used on an associative array returning first l

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kirstenska
Forum Newbie
Posts: 7
Joined: Mon Jun 21, 2010 1:50 am
Location: Santa Clara, CA (silicon valley, baybee!)

Re: foreach() used on an associative array returning first l

Post 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!
Post Reply