How do you define an Array in PHP? write a PHP script to create a multidimensional array name Product that will contain pcode, pname. and price. Initialize the array with at least three instances. Also, Write an HTML script to display the initialized values of the array in an HTML Table.

This answer is restricted. Please login to view the answer of this question.

Login Now

An array is a data structure that stores one or more similar types of values in a single value. For example, if you want to store 100 numbers then instead of defining 100 variables it’s easy to define an array of 100 lengths.

In PHP, the array() the function is used to create an array:

array();

Example:

$developers = array("Suresh", "Bipin", "Prince");

In 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 indexes.

PHP script to create a multidimensional array name Product that will contain pcode, pname. and price is

//initialize multidimensional array with pcode, pname and price
$product = array(
   "pcode" => array(),
   "pname" => array(),
   "price" => array(),
);

//initialize array with three values
$product["pcode"] = array("CHP12", "CHP13", "CHP14");
$product["pname"] = array("Adblock Detector", "Subscription Plan", "All Smart Tools");
$product["price"] = array(2500, 300, 50000);

Now, We will render this array in HTML code. Below is the full code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Render PHP Array to HTML Table</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Product Code</th>
                <th>Product Name</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
        <?php
            //initialize multidimensional array with pcode, pname and price
            $product = array(
                "pcode" => array(),
                "pname" => array(),
                "price" => array()
            );
        
            //initialize array with three values
            $product["pcode"] = array("CHP12", "CHP13", "CHP14");
            $product["pname"] = array("Adblock Detector", "Subscription Plan", "All Smart Tools");
            $product["price"] = array(2500, 300, 50000);

            $totalvalues = count( $product['pcode'] );
            $arrayvaluesonly = array_values( $product );
            for( $i = 0; $i < $totalvalues; $i++ ){
                echo sprintf('<tr>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%d</td>
                </tr>', $arrayvaluesonly[0][$i], $arrayvaluesonly[1][$i], $arrayvaluesonly[2][$i]);
            }
        ?>
        </tbody>
    </table>
</body>
</html>

The output of the above code is

Product Code Product Name Price
CHP12 Adblock Detector 2500
CHP13 Subscription Plan 300
CHP14 All Smart Tools 50000
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .