Array PHP multidimensi


array
//----------------------------------------------------------------------------
|
|
|
http://www.journaldev.com/1528/php-arrays-indexed-associative-and-multidimensional-arrays

http://webcheatsheet.com/php/multidimensional_arrays.php  
|
|
|
//-----------------------------------------------------------------------------

laravel
http://stackoverflow.com/questions/18079240/validating-multiple-file-uploads-with-laravel-4

tutor
https://ellislab.com/forums/viewthread/110130/

http://forum.kohanaframework.org/discussion/3261/validation-of-multiple-file-uploads/p1
-----------------
html5
http://stackoverflow.com/questions/3215324/html-5-multi-file-upload-with-php

PHP: Multidimensional Arrays
Print

Array does not have to be a simple list of keys and values; each array element can contain another array as a value, which in turn can hold other arrays as well. In such a way you can create two-dimensional or three-dimensional array.

    Two-dimensional Arrays
    Three-dimensional Arrays

Two-dimensional Arrays

Imagine that you are an owner of a flower shop. One-dimensional array is enough to keep titles and prices. But if you need to keep more than one item of each type you need to use something different - one of the ways to do it is using multidimensional arrays. The table below might represent our two-dimensional array. Each row represents a type of flower and each column – a certain attribute.

Title Price Number
rose 1.25 15
daisy 0.75 25
orchid 1.15 7

To store data in form of array represented by preceding example using PHP, let’s prepare the following code:

<?php
$shop = array( array("rose", 1.25 , 15),
               array("daisy", 0.75 , 25),
               array("orchid", 1.15 , 7)
             );
?>

This example shows that now $shop array, in fact, contains three arrays.  As you remember, to access data in one-dimensional array you have to point to array name and index. The same is true in regards to a two-dimensional array, with one exception: each element has two indexes – row and column.

To display elements of this array we could have organize manual access to each element or make it by putting For loop inside another For loop:

<?php
echo "<h1>Manual access to each element</h1>";

echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]."<br />";
echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]."<br />";

echo "<h1>Using loops to display array elements</h1>";

echo "<ol>";
for ($row = 0; $row < 3; $row++)
{
    echo "<li><b>The row number $row</b>";
    echo "<ul>";

    for ($col = 0; $col < 3; $col++)
    {
        echo "<li>".$shop[$row][$col]."</li>";
    }

    echo "</ul>";
    echo "</li>";
}
echo "</ol>";
?>

Perhaps, instead of the column numbers you prefer to create their names. For this purpose, you can use associative arrays.  The following code will store the same set of flowers using column names:

<?php
$shop = array( array( Title => "rose",
                      Price => 1.25,
                      Number => 15
                    ),
               array( Title => "daisy",
                      Price => 0.75,
                      Number => 25,
                    ),
               array( Title => "orchid",
                      Price => 1.15,
                      Number => 7
                    )
             );
?>

It is easier to work with this array, in case you need to get a single value out of it. Necessary data can be easily found, if you turn to a proper cell using meaningful row and column names that bear logical content.  However, we are loosing the possibility to use simple for loop to view all columns consecutively.

You can view outer numerically indexed $shop array using the for loop. Each row of the $shop array is an associative array.  Hence, inside the for loop you need for each loop.  Also you can get each element from associative array manualy:

<?php
echo "<h1>Manual access to each element from associative array</h1>";

for ($row = 0; $row < 3; $row++)
{
    echo $shop[$row]["Title"]." costs ".$shop[$row]["Price"]." and you get ".$shop[$row]["Number"];
    echo "<br />";
}

echo "<h1>Using foreach loop to display elements</h1>";

echo "<ol>";
for ($row = 0; $row < 3; $row++)
{
    echo "<li><b>The row number $row</b>";
    echo "<ul>";

    foreach($shop[$row] as $key => $value)
    {
        echo "<li>".$value."</li>";
    }

    echo "</ul>";
    echo "</li>";
}
echo "</ol>";
?>

Back to top

Three-dimensional Arrays

You don’t have to be limited by two dimensions: the same way as array elements can contain other arrays, these arrays, in their turn, can contain new arrays.

Three-dimensional array is characterized by height, width, and depth. If you feel comfortable to imagine two-dimensional array as a table, then imagine a pile of such tables.  Each element can be referenced by its layer, row, and column.

If we classify flowers in our shop into categories, then we can keep data on them using three-dimensional array. We can see from the code below, that three-dimensional array is an array containing array of arrays:

<?php
$shop = array(array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7)
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7)
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7)
                   )
             );
?>

As this array has only numeric indexes, we can use nested for loops to display it:

<?php
echo "<ul>";
for ( $layer = 0; $layer < 3; $layer++ )
{
    echo "<li>The layer number $layer";
    echo "<ul>";
  
    for ( $row = 0; $row < 3; $row++ )
    {
       echo "<li>The row number $row";
       echo "<ul>";
    
        for ( $col = 0; $col < 3; $col++ )
        {
            echo "<li>".$shop[$layer][$row][$col]."</li>";
        }
        echo "</ul>";
        echo "</li>";
    }
    echo "</ul>";
    echo "</li>";
}
echo "</ul>";
?>

This way of creating multidimensional arrays allows to create four- and five-dimensional arrays.
Syntax rules do not limit the number of dimensions, but the majority of practical tasks logically correspond
to the constructions of three or less dimensions.

An array is a group of similar types of variables. PHP provides function array() to create an array.

There are three types of arrays in PHP.

    Indexed arrays – Array with numeric indexes.
    Associative arrays – Array with key-value pairs, its similar to Map in java.
    Multidimensional arrays – An array of arrays.

Indexed arrays

There are two ways to create indexed arrays.
– first way to use array() function without any index, index are assigned automatically starting from 0.
– second way to manually assign index and create the array.

PHP count() function is used to get the length of an array. We can use for loop to loop through all the values of an indexed array.

Below code shows both the ways to create an indexed array and loop through them in PHP.

<?php
$colors = array("Red","Green","Blue");

$colors1[0] = "Red";
$colors1[1] = "Green";

$length = count($colors);
echo "colors array length=" . $length; // prints "colors array length=3"
echo "<br>";
echo "colors1 array length=" . count($colors1); // prints "colors1 array length=2"

//looping an indexed array
for($i=0; $i<$length; $i++){
echo $colors[$i];
echo "<br>";
}
?>

Associative Arrays

Associative arrays uses named keys for values and we can create them in similar way like indexed arrays. foreach is used to loop through an associative array.

<?php

$colors = array("0"=>"Red","1"=>"Green","2"=>"Blue");

echo "0th element of array is " . $colors["0"];
echo "<br>";
//looping
foreach ($colors as $key=>$value){
echo "Key=".$key." value=".$value;
echo "<br>";
}
?>

output of above PHP script is:

0th element of array is Red
Key=0 value=Red
Key=1 value=Green
Key=2 value=Blue

Multidimensional Arrays

A multidimensional array is an array of arrays. We can create two-dimensional, three-dimensional and n-dimensional arrays using array function.

Each array within the multidimensional array can be either indexed array or associative array. We can use for loop for looping through indexed array and foreach for looping through associative array.

In below example, I am showing two-dimensional indexed arrays and mixture of indexed and associative array in multidimensional array.

We can use print_r() or var_dump() function to print the human readable form of the array.

<?php

//Indexed two-dimensional array
$cars = array(
array("Honda Accord", "V6", 30000),
array("Toyota Camry", "LE", 24000),
array("Nissan Altima", "V1"),
);

//printing array information
print_r($cars);
echo "<br>";
var_dump($cars);
echo "<br>";
//looping through two-dimensional indexed array

for($i=0;$i<count($cars);$i++){
for($j=0;$j<count($cars[$i]);$j++){
echo $cars[$i][$j] . " ";
}
echo "<br>";
}
echo "<br><br>";

//Indexed Associative two-dimensional array
$cars = array(
array("Name" => "Honda Accord", "Model" => "V6", "Cost" => 30000),
array("Name" => "Toyota Camry", "Model" => "LE", "Cost" => 24000),
array("Name" => "Nissan Altima", "Model" => "V1"),
);

//printing array information
print_r($cars);
echo "<br>";
var_dump($cars);
echo "<br>";

//looping through two-dimensional indexed associative array

for($i=0;$i<count($cars);$i++){
$c=0;
foreach($cars[$i] as $key=>$value){
$c++;
echo $key."=".$value;
if($c<count($cars[$i])) echo ",";
}
echo "<br>";
}
?>

The output of above PHP script is:

Array ( [0] => Array ( [0] => Honda Accord [1] => V6 [2] => 30000 ) [1] => Array ( [0] => Toyota Camry [1] => LE [2] => 24000 ) [2] => Array ( [0] => Nissan Altima [1] => V1 ) )
array(3) { [0]=> array(3) { [0]=> string(12) "Honda Accord" [1]=> string(2) "V6" [2]=> int(30000) } [1]=> array(3) { [0]=> string(12) "Toyota Camry" [1]=> string(2) "LE" [2]=> int(24000) } [2]=> array(2) { [0]=> string(13) "Nissan Altima" [1]=> string(2) "V1" } }
Honda Accord V6 30000
Toyota Camry LE 24000
Nissan Altima V1


Array ( [0] => Array ( [Name] => Honda Accord [Model] => V6 [Cost] => 30000 ) [1] => Array ( [Name] => Toyota Camry [Model] => LE [Cost] => 24000 ) [2] => Array ( [Name] => Nissan Altima [Model] => V1 ) )
array(3) { [0]=> array(3) { ["Name"]=> string(12) "Honda Accord" ["Model"]=> string(2) "V6" ["Cost"]=> int(30000) } [1]=> array(3) { ["Name"]=> string(12) "Toyota Camry" ["Model"]=> string(2) "LE" ["Cost"]=> int(24000) } [2]=> array(2) { ["Name"]=> string(13) "Nissan Altima" ["Model"]=> string(2) "V1" } }
Name=Honda Accord,Model=V6,Cost=30000
Name=Toyota Camry,Model=LE,Cost=24000
Name=Nissan Altima,Model=V1



https://ellislab.com/forums/viewthread/110130/

0 Response to "Array PHP multidimensi"

Posting Komentar