Solved

PHP array question

Posted on 2014-11-19
5
79 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 82

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 82

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

13 Experts available now in Live!

Get 1:1 Help Now