Solved

PHP array question

Posted on 2014-11-19
5
82 Views
Last Modified: 2014-11-19
I have an inherited script that has the loop:

      
while($row = mysql_fetch_array($result)) {
		$result_array[] = $row;
	}
         foreach ($result_array as $result) {
        ... }

Open in new window


what is the benefit of splitting this seemingly common/conventional (mysql results) loop into two loops?
and doesn't the first loop just copy the array, row by row?
0
Comment
Question by:willsherwood
  • 2
  • 2
5 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40452987
what is the benefit
There isn't any from what you have posted, maybe an inexperienced programmer who didn't understand the results are an array.
0
 

Author Closing Comment

by:willsherwood
ID: 40453462
thanks for the confirmation
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40453488
Actually, that isn't true.  That method gets all the data from MySQL at once and puts it into an array of arrays.  It's not just 'copying' it.  Then the 'foreach' is used to go thru the results to do what you want.

The functions of getting the data from the database and generating the display for the page are broken into two separate sections this way.  It also clears all the data from the database query so you can reuse that query.  Until all the data is retrieved, you can't 're-use' that query, you would have to make a new one.

This is even more useful when you have more than one database array to deal with.  I have pages that get three different arrays from the database that are then used for different sections of the page.  The second and third arrays are dependent on content from the first one.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40453502
Well if you are sticking with the old mysql library it would be easier to just use fetch_array
But the answer was based on the posted code
(or better still move to PDO)
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40453522
I've moved everything to 'mysqli'.  It has nothing really to do with the particular library but the method that is being used.  I use it a lot for the reasons I stated.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now