Close Menu
    Facebook X (Twitter) Instagram
    Oixiesoft
    • Home
    • Services
      • WordPress Malware Removal
      • Fix WordPress Errors
      • WordPress Website Development
    • Articles
    • Contact
    Oixiesoft
    Home»Tutorial»PHP»PHP Arrays
    PHP

    PHP Arrays

    Editorial StaffBy Editorial StaffUpdated:May 4, 2022No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    What is an Array?

    An array is an extraordinary variable, that can store more than one value or a set of values at a time.

    It provides fewer code characteristics because we can specify a single variable instead of multiple variables. The array also defines sorting features.

    Example :

    In this example we can hold multiple value in multiple variables.

    <!DOCTYPE html>
    <html>
    <body>
    <?php
    // This is a multiple variable
    $colors1 = "Red";
    $colors2 = "Pink";
    $colors3 = "Yellow";
    echo "My favorite colors is " . $colors1 . ", " . $colors2 . " and " . $colors3 . ".";
    ?>
    
    </body>
    </html>

    Output

    My favorite colors is Red, Pink, Yellow.

    Example :

    In this example we can hold multiple value in single variable.

    <!DOCTYPE html>
    <html>
    <body>
    <?php
    
    // This is a single variable
    $colors = array("Red", "Pink", "Yellow"); 
    echo "MY favorite colors is " . $colors[0] . ", " . $colors[1] . " and " . $colors[2] . ".";
    ?>
    </body>
    </html>

    Output

    My favorite colors is Red, Pink, Yellow.

    PHP Array Types

    There is three distinct types of arrays which is given below:

    • Indexed array
    • Associative array
    • Multidimensional array

    PHP Indexed array

    An array with a numeric index that begins from 0. In an indexed array, each element’s values are kept and accessed in a linear fashion.
    There are two ways to define an indexed array, the simplest way is:

    Example: 1st way

    <?php
    $colors=array("Red","Pink","Yellow");  ?>
    

    Example: 2nd way

    <?php
    $colors[0] = "Red"; 
    $colors[1] = "Pink"; 
    $colors[2] = "Yellow"; ?>
    

    PHP Associative Arrays

    The associative arrays are very equivalent to numeric arrays in terms of functionality. But they are different in the words of their index. Associative array will have their index as a string so that you can specify a strong connection between keys and values.

    For example, if you want to accumulate the salary of employees in an array, a numerically indexed array would not be the best choice.
    Rather, we could use the employees’ names as the keys in our associative array, and the value would be their respective salaries.

    NOTE: Don’t keep associative array inside double quote while printing otheriwse it would not return any value.

    Example

    <html>
    <body>
    <?php
             /* First method to associate create array. */
             $salary = array("John" => 2200, "Carrillo" => 1200, "James" => 700);
             
             echo "Salary of John is ". $salary['John'] . "<br />";
             echo "Salary of Carrillo is ".  $salary['Carrillo']. "<br />";
             echo "Salary of James is ".  $salary['James']. "<br /><br />";
             
             /* Second method to create array. */
             $salary['John'] = "high";
             $salary['Carrillo'] = "medium";
             $salary['James'] = "low";
             
             echo "John's salary is ". $salary['John'] . "<br />";
             echo "Carrillo's salary is ".  $salary['Carrillo']. "<br />";
             echo "James' salary is ".  $salary['James']. "<br />";
          ?>
    </body>
    </html>
    

    Output

    The result will here −

    Salary of John is 2200
    Salary of Carrillo is 1200
    Salary of James is 700

    John’s salary is high
    Carrillo’s salary is medium
    James’ salary is low

    PHP – Multidimensional Arrays

    In a multidimensional array, the main array can also contain one or more arrays that are two, three, four, five, or more levels deep.

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Tumblr Email
    Editorial Staff

    Related Posts

    CodeIgniter vs Laravel: A Detailed Side-by-Side Comparison

    Python Arrays

    Python Lambda

    PHP
    • PHP Comments
    • PHP if else Statements
    • The PHP switch Statement
    • PHP Functions
    • PHP for Loop
    • PHP Arrays
    • PHP Include Files
    • PHP Date() and Time() Function
    • PHP Forms Tutorial
    Services
    • Web Development
    • Mobile Application Development
    • WordPress Malware Removal Service
    • Website Design
    • WordPress Development
    • Magento Development
    • Shopify Development
    • SEO Services
    Blog
    • How to Fix the Error Establishing a Database Connection
    • Ping List WordPress
    • How To Fix Japanese Keyword Hack
    • How to remove Malware from WordPress
    Hire Developers
    • Hire WordPress Developer
    • Hire Shopify Developer
    Contact Info
    • Oixiesoft Technologies
      A-40, Block A, I thum Tower, Sector 62, Noida
    • [email protected]
    • Privacy Policy
    • About Us
    • Contact Us
    © 2025 OixieSoft Technologies

    Type above and press Enter to search. Press Esc to cancel.