Array in PHP

PHP Arrays
  • Array stores one or more similar type multiple values in one single variable.
  • An array is a special type of variable, which can hold one or more value at a time in a variable.
  • PHP arrays have three types.
  • example
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $bike = array("BMW", "Yamaha", "suzuki");
    print_r ($bike);
    ?>
    </body>
    </html>
    
1.PHP Numeric Array
  • Numeric Array can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.
  • example
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $num = array( 10,20,30);
    foreach( $num as $val ) {
    echo "array $val </br>";
    }
    ?>
    </body>
    </html>
    
  • another example
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $num[0] = "ten";
    $num[1] = "twenty";
    $num[2] = "thirty";
    
    foreach( $num as $val ) {
    echo "array value - $val </br>";
    }
    ?>
    </body>
    </html>
    
2.PHP Associative Arrays
  • Associative arrays are similar to numeric arrays in terms of functionality but they are different in terms of their index.
  • It will have their index as string so that you can establish a strong union between keys and values.
  • example
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $bike_price = array("BMW S 1000 RR"=>1850000, "BMW F 900 R"=>990000, "BMW R 1250 RT"=>2250000);
    echo "Price of BMW S 1000 RR is ". $bike_price['BMW S 1000 RR'] . "</br>";
    echo "Price of BMW F 900 R is ".  $bike_price['BMW F 900 R']. "</br>";
    echo "Price of BMW R 1250 RT is ".  $bike_price['BMW R 1250 RT']. "</br>";
    ?>
    </body>
    </html>
    


  • another example
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $bike_price['BMW S 1000 RR'] = 1850000;
    $bike_price['BMW F 900 R'] = 9900000;
    $bike_price['BMW R 1250 RT'] = 2250000;
    
    echo "Price of BMW S 1000 RR is ". $bike_price['BMW S 1000 RR'] . "</br>";
    echo "Price of BMW F 900 R is ".  $bike_price['BMW F 900 R'] . "</br>";
    echo "Price of BMW R 1250 RT is ".  $bike_price['BMW R 1250 RT'] . "</br>";
    ?>
    </body>
    </html>
    
3.PHP Multidimensional Arrays
  • A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
  • example
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $bike_price = array("BMW S 1000 RR"=>1850000, "BMW F 900 R"=>990000, "BMW R 1250 RT"=>2250000);
    echo "Price of BMW S 1000 RR is ". $bike_price['BMW S 1000 RR'] . "</br>";
    echo "Price of BMW F 900 R is ". $bike_price['BMW F 900 R']. "</br>";
    echo "Price of BMW R 1250 RT is ". $bike_price['BMW R 1250 RT']. "</br>";
    ?>
    </body>
    </html>
    
  • another example
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $bike_price = array(
    "BMW" => array (
    "G 310 R" => 200000,
    "S 1000 R" => 1670000,
    ),
    "Royal Enfield" => array (
    "Himalayan" => 194000,
    "Bullet 350" => 131000,
    ),
    "Suzuki" => array (
    "Gixxer SF" => 130000,
    "Hayabusa" => 1374000,
    )
    );
    
    echo "Price :- BMW => G 310 R : " ;
    echo $bike_price['BMW']['G 310 R'] . "</br>";
    echo "Price :- BMW => S 1000 R : " ;
    echo $bike_price['BMW']['S 1000 R'] . "</br></br>";
    
    echo "Price :- Royal Enfield => Himalayan : ";
    echo $bike_price['Royal Enfield']['Himalayan'] . "</br>";
    echo "Price :- Royal Enfield => Bullet 350 : ";
    echo $bike_price['Royal Enfield']['Bullet 350'] . "</br></br>";
    
    echo "Price :- Suzuki => Gixxer SF : " ;
    echo $bike_price['Suzuki']['Gixxer SF'] . "</br>";
    echo "Price :- Suzuki => Hayabusa : " ;
    echo $bike_price['Suzuki']['Hayabusa'] . "</br>";
    
    ?>
    </body>
    </html>
    
    PHP - Sort Functions For Arrays
  • In Array, the stored elements in alphabetical or numerical order, ascending or descending.
  • Array Sort Functions are given below.
    • sort() sort array in ascending order
    • rsort() sort arrays in descending order
    • ksort() sort arrays in ascending order, as per to the key
    • asort() sort arrays in ascending order, as per to the value
    • arsort() sort arrays in descending order, as per to the value
    • krsort() sort associative arrays in descending order, as per to the key
  • Example.
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $bike = array("BMW", "Yamaha", "Suzuki");
    sort($bike);
    $clength = count($bike);
    for($x = 0; $x < $clength; $x++) {
    echo $bike[$x] ."</br>";
    }
    ?>
    </body>
    </html>
    

Exercise: Other PHP array function list click here for see list.