Jump to content

What they used for?


Recommended Posts

lol obviously have not worked much with data.

 

Arrays are very handy in many instances, whether it would be storing state data from a database or just in code IE:

 

<?php
$states = array("al" => "Alaska", "CO" => "Colorado");

// Using the array above I can easily loop through it and print out the data in a fashion I like
foreach ($states as $abb => $full) {
    echo $abb . ' is the abbreviation for ' . $full . '<br />';
}

// I can also take in just an abbreviation and find that state name

echo $states['al'] . " is the state for the abbreviation 'al'<br />";

//Or I can take it a step further and house item data:
$items = array(0 => array("item_name" => "Shoes", "price" => "25.00", "quantity" => 32), 1 => array("item_name" => "Robes", "price" => "25.00", "quantity" => 3));
$total = 0;
foreach ($items as $id => $item) {
      $item_total = $item['price'] * $item['quantity'];
      $total += $item_total;
      echo "You have chosen to buy " . $item['item_name'] . " at the cost of \$" . $item['price'] . " with a quantity of " . $item['quantity'] . " which is equal to \$" . number_format($item_total, 2) . "<br />";
}
echo '<br />Your orders total is $' . number_format($total);
?>

 

Really arrays can make processing large amounts of data easier and efficiently, as you can see I could have 50 items in that array and it will total everything up and print out nice messages to me with few lines of code.

Link to comment
Share on other sites

lol, now the question is, let's say that you retrieve the data from mysql, what format does it come in? Usually you use mysql_fetch_array to retrieve data from mysql. Weird it comes in an array format...

 

Now let's say that you want to process the data from mysql somewhere else on the page instead of just printing it out within the while loop. Say we have state data that could be used multiple times on the page. We can gather the data from mysql into a multi-dimensional array which we then can pass to different functions, say one function is to create a drop down menu, another function is to assign flags to each of the states and the third function is for let's say assigning the state flower.

 

Now you could make 3 trips to the database grabbing the same data, or you could just assign the data to an array and loop through the array, thus saving mysql retrival time.

 

But when you think of that question ask yourself, how is $_SESSION data retrieved and stored? How is $_COOKIE data retrieve and store? Same with GET and POST, also what about $_SERVER or $_REQUEST?

 

All are arrays. It just makes life easier to access them that way. Although it may not be apparent it, I guarantee you, that you will one day realize, wow arrays can make my job a ton easier. I would much rather have my data in an array as appose to multiple variables. I can easily change the index name of an array, the data etc without having to get creative =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.