Arrays PHP

View: 580    Dowload: 0   Comment: 0   Post by: hanhga   Category: Forum PHPBB, VBB   Fields: Other

Arrays can be used in many ways to store and organize data quickly and efficiently. It is one of the more useful data types available to any programming language.

Arrays can most easily be described as an ordered list of elements. You can access the individual elements by referring to their index position within the array. The position is either specified numerically or by name. An array with a numeric index is commonly called an indexed array while one that has named positions is called an associative array. In PHP, all arrays are associative, but you can still use a numeric index to access them.

An Example of an indexed Array:

<?php 
$seven = 7; 
$arrayname = array( "this is an element", 5, $seven ); 

echo $arrayname[0];   //prints: this is an element 
echo $arrayname[1];   //prints: 5 
echo $arrayname[2];   //prints: 7
?>

As you can see, elements in an array can be any type of data (string, integer, double) and can also be other variables. An individual array element can be of any type, including another array.If you want to find out if a variable contains an array you can use the is_array() function. Notice that Indexed arrays start at position zero, not at position one, so your first array element has an index of 0, and the highest position in the array is one less than the number of elements in the array

Associative Arrays :

Associative arrays are arrays that use named keys that you assign to them. Have a look at the following example:

<?php 
$first_array = array("key1" => "the first element", "key2" => "the second element"); 

$second_array = array( 
    "key3" => "this is the first element of the second array", 
    "key4" => "this is the second element of the second array", 
); 
echo $first_array['key1'];    //prints "the first element." 
echo $second_array['key3'];   //prints "the first element of the second array" 
echo $first_array['key2'];    //prints "the second element" 
echo $second_array['key4'];   //prints "this is the second element of the second array" 
?>

Right, now you know how to define an associative array, but you probably don't see yet how useful are they. Well think of this, say you have a flower-shop. You have 3 different flowers, and each flower has a different price. Let's make this example in php.

<?php 
//We initialize the array using the array() function. 
//Note that for readability one can spread the argument over several lines.

$flower_shop = array ( 
     "rose" => "5.00", 
     "daisy" => "4.00", 
     "orchid" => "2.00"
); 

echo "rose costs $flower_shop['rose'], daisy costs $flower_shop['daisy'], and orchild costs $flower_shop['orchild']."; 
?>

Because the indices in this associative array are not numbers, we cannot use a simple counter in a for loop to work with the array. We can use the foreach loop. In the following example we use the foreach loop to iterate through our flowers_shop array, and read them into a table. Note carefully the syntax.

<?php
//We initialize the array using the array() function. 
//Note that for readability one can spread the argument over several lines.

$flower_shop = array ( 
     "rose" => "5.00", 
     "daisy" => "4.00", 
     "orchid" => "2.00", 
); 
//let's print out the headers to our table
echo "<table border='1' cellpadding='5'>";
echo"<tr><th>Flower</th><th>Price</th></tr>";
//Now we start the foreach loop using the variable $flower to hold our key 
//and $price to hold our cost.

foreach($flower_shop as $Flower=>$Price)
{
  echo "<tr><td>$Flower </td><td>$Price</td></tr> "; //print the values into a table cell for each iteration
}
//finally close the table
echo "</table>";
?> 

Multidimensional Arrays

In preceding example you've learned how to use arrays. But what if you want to give more information on each flower? You now have the cost, but what if you wanted to add the number of flowers you get for that price, and the colour of the flower? One of the ways to do it is using multidimensional arrays.

A multidimensional array is an array that contains at least one other array as the value of one of the indexes. Example below shows how to use multidimensional array:

<?php 
//Initialize the array using the array() function.
$flower_shop = array( 
"rose" => array( "5.00", "7 items", "red" ), 
"daisy" => array( "4.00", "3 items", "blue" ), 
"orchid" => array( "2.00", "1 item", "white" ), 
); 

//print "rose costs 5.00, and you get 7 items." 
echo "rose costs ".$flower_shop['rose'][0].", and you get ".$flower_shop['rose'][1]."."; 
//print "daisy costs 4.00, and you get 3 items." 
echo "daisy costs ".$flower_shop['daisy'][0].", and you get ".$flower_shop['daisy'][1]."."; 
//print "orchild costs 2.00, and you get 1 item. 
echo "orchid costs ".$flower_shop['orchid'][0].", and you get ".$flower_shop['orchild'][1]."."; 
?>

 

 

Arrays PHP

Arrays can be used in many ways to store and organize data quickly and efficiently. It is one of the more useful data types available to any programming language.

Posted on 15-12-2015 

Comment:

To comment you must be logged in members.

Files with category

  • Input Validation Using Filter Functions with php

    Input Validation Using Filter Functions with php

    View: 575    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    Filter functions in PHP might not be sexy, but they can improve the stability, security, and even maintainability of your code if you learn how to use them correctly.

  • The Open/Closed Principle in PHP

    The Open/Closed Principle in PHP

    View: 445    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    As usual, a good way to grasp what’s actually behind the curtain of the principle and how to take advantage of its benefits is by example. In this article I’ll be setting up a few approachable ones in an attempt to unveil the principle’s driving...

  • Dependency Injection with Pimple in PHP

    Dependency Injection with Pimple in PHP

    View: 502    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    In application development, we try to create independent modules so that we can reuse code in future projects. But, it’s difficult to create completely independent modules which provide useful functionality; their dependencies can cause maintenance...

  • Working with Directories in PHP

    Working with Directories in PHP

    View: 641    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    Let's start with simple listing the contents of a directory. We need three functions to perform this task: opendir(), readdir() and closedir(). The opendir() function takes one parameter, which is the directory we want to read, and returns a...

  • Guide to Email (Text / HTML / Attachments) in PHP

    Guide to Email (Text / HTML / Attachments) in PHP

    View: 619    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters.

  • Dynamic Image Generation in PHP

    Dynamic Image Generation in PHP

    View: 525    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    At first, have a look at the following script that creates captcha image. Next, we will consider each step of image generation in details.

  • Form Processing with PHP

    Form Processing with PHP

    View: 856    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    Before you can process the information, you need to create an HTML form that will send information to your PHP script. There are two methods for sending data: POST and GET. These two types of sending information are defined in your HTML form...

  • Function in PHP

    Function in PHP

    View: 739    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    A function is a block of code which can be called from any point in a script after it has been declared. It is basically a compartmentalized PHP script designed to accomplish a single task. Furthermore, code contained within functions is ignored...

 
File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd