Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-21-2011, 03:05 PM Arrays
madnhain's Avatar
Experienced Talker

Posts: 42
Name: Jez
Location: Grand Junction, CO / Williston, ND
Trades: 0
I'm barely an intermediate level php programmer and was asked the other day by a novice guy what use arrays actually have when mysql is easy to use, and can store stuff on a permanent basis.

Granted, arrays can write and recall information from text files for example, but it still seems that storing information is just more convenient with mysql.

What would a practical application of a PHP array be? I know this may seem like a stupid question, but I've never had a need to use an array.
madnhain is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-21-2011, 03:25 PM Re: Arrays
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
One use is reading data from the database into its own array. The array keys would presumably match the column field names.

It is easier, and more readable, to use an array in this manner rather than individual $table_field variables set up for them. For instance, try iterating through a result set (which is probably returned in an array to begin with) with a foreach( $results as $ result ) loop.

At any rate, arrays are used to hold the data that is returned from the database query. Where else would you put the results?

Arrays are also good for setting up temporary datasets before implementing into an actual database, or even using them outright for such a purpose. Lets say you have a limited amount of locations that you want to concentrate on with city, county, zipcode keys and there are only twenty or so results for that array. It is simpler to create a static array than going through a database.
__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]

Last edited by RonnieTheDodger; 02-21-2011 at 03:27 PM..
RonnieTheDodger is offline
Reply With Quote
View Public Profile Visit RonnieTheDodger's homepage!
 
Old 02-21-2011, 03:30 PM Re: Arrays
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
Well, a db SELECT usually returns data in an array ... However, tell novice guy he's comparing apples and oranges and should keep quiet - he's comparing databased storage with a programming construct - not a valid comparison. What if you want to pass a lot of values to a function - use an array - any time when you need to reference a lot of values in a singular way - use an array. What if you want to carry out more exotic manipulations via multi-dimensional arrays? Tell NG to go read a basic programming algorithms text.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Old 02-21-2011, 03:49 PM Re: Arrays
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
Quote:
Originally Posted by PaulW View Post
What if you want to pass a lot of values to a function - use an array - any time when you need to reference a lot of values in a singular way - use an array.
That is another good use. Something that more than one WordPress plugin developers should learn when it comes to storing their option values. I have seen time, and time again, where they store each option inividually rather than lumping them into one options array and writing to disk once. In this instance, it saves resources and takes less of a performance hit.
__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]
RonnieTheDodger is offline
Reply With Quote
View Public Profile Visit RonnieTheDodger's homepage!
 
Old 02-21-2011, 05:05 PM Re: Arrays
Banned

Posts: 4
Name: victorwan
Trades: 0
PHP array is the actually map, A map is a type to associates values to keys. This type is optimized for several different uses. Array uses list of vector, hash table, dictionary, collection, stack, queue and probablly
victorwan is offline
Reply With Quote
View Public Profile
 
Old 02-21-2011, 05:07 PM Re: Arrays
Skilled Talker

Posts: 83
Trades: 0
correct me if i'm wrong,

i think you are talking about string arrays.

well simplest difference of array with database is arrays are stored in heap.
__________________
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
»
Please login or register to view this content. Registration is FREE
for web developers
DmitryS is offline
Reply With Quote
View Public Profile
 
Old 02-21-2011, 10:11 PM Re: Arrays
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
Quote:
Originally Posted by DmitryS View Post
correct me if i'm wrong,

i think you are talking about string arrays.

well simplest difference of array with database is arrays are stored in heap.
Well Victor did an "almost quote" right out of the Php manual which says:

Quote:
Arrays

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]
RonnieTheDodger is offline
Reply With Quote
View Public Profile Visit RonnieTheDodger's homepage!
 
Reply     « Reply to Arrays
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50896 seconds with 12 queries