Open In App

How to display array structure and values in PHP ?

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 



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
  
// Array with subjects
$array1 = array(
      '0' => "Python"
      '1' => "java"
      '2' => "c/cpp"
);
  
// Display array structure
var_dump($array1);
  
?>

Output
array(3) {
  [0]=>
  string(6) "Python"
  [1]=>
  string(4) "java"
  [2]=>
  string(5) "c/cpp"
}

Here, 

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:

Return Value: Array with values.

Example: PHP program to display an array of values.




<?php
  
// Array with subjects
$array1 = array(
      '0' => "Python"
      '1' => "java"
      '2' => "c/cpp"
);
  
// Display array values
print_r($array1);
  
?>

Output
Array
(
    [0] => Python
    [1] => java
    [2] => c/cpp
)

Article Tags :