Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP array question

Posted on 2014-11-19
5
Medium Priority
?
99 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 2000 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 84

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 84

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

772 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