How to display array structure and values in PHP ?
Last Updated :
07 Oct, 2021
In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions.
It includes
- Array size
- Array values
- Array value with Index
- Each value data type
We will display the array structure using var_dump() function. This function is used to dump information about a variable. This function displays structured information such as the type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure. This function is also effective with expressions.
Syntax:
var_dump( $array_name )
Parameters: The function takes a single argument $array_name that may be one single variable or an expression containing several space-separated variables of any type.
Return Type: Array Structure
Example: PHP Program to create an array and display its structure.
PHP
<?php
// Array with subjects
$array1 = array(
'0' => "Python",
'1' => "java",
'2' => "c/cpp"
);
// Display array structure
var_dump($array1);
?>
Outputarray(3) {
[0]=>
string(6) "Python"
[1]=>
string(4) "java"
[2]=>
string(5) "c/cpp"
}
Here,
- array(3) is the size of the array ( 3 elements)
- string(6) is the data type of element 1 with its size
- string(4) is the data type of element 2 with its size
- string(5) is the data type of element 3 with its size
Array Values: We will display the array values by using the print_r() function. This function is used to print or display information stored in a variable.
Syntax:
print_r( $variable, $isStore )
Parameters:
- $variable: This parameter specifies the variable to be printed and is a mandatory parameter.
- $isStore: This is an optional parameter. This parameter is of boolean type whose default value is FALSE and is used to store the output of the print_r() function in a variable rather than printing it. If this parameter is set to TRUE then the print_r() function will return the output which it is supposed to print.
Return Value: Array with values.
Example: PHP program to display an array of values.
PHP
<?php
// Array with subjects
$array1 = array(
'0' => "Python",
'1' => "java",
'2' => "c/cpp"
);
// Display array values
print_r($array1);
?>
OutputArray
(
[0] => Python
[1] => java
[2] => c/cpp
)
Similar Reads
How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he
2 min read
How to display string values within a table using PHP ? A table is an arrangement of data in rows and columns, or possibly in a more complex structure. Tables are widely used in communication, research, and data analysis. Tables are useful for various tasks such as presenting text information and numerical data.Tables can be used to compare two or more i
1 min read
How to Populate Dropdown List with Array Values in PHP? We will create an array, and then populate the array elements to the dropdown list in PHP. It is a common task when you want to provide users with a selection of options. There are three approaches to achieve this, including using a foreach loop, array_map() function, and implode() function. Here, w
2 min read
How to use array_merge() and array_combine() in PHP ? In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or
3 min read
How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an
2 min read