Home >Backend Development >PHP Problem >How to output php array object

How to output php array object

PHPz
PHPzOriginal
2023-04-27 09:08:251454browse

In the PHP language, arrays and objects are very important data types. At the same time, in many cases, we need to output the array or object for other programs or people to view. Therefore, this article will explain how to output PHP arrays and objects.

1. Output array

The array in PHP is an ordered set of data, which can be represented by a pair of square brackets. Arrays can contain any type of data, such as strings, numbers, and other arrays. In PHP, we can use echo or print_r function to output an array.

1. Use echo to output an array

When we use the echo function to output an array, PHP will convert the array into a string and display it on the screen. But this method does not show the structure of the array very well, because it just connects the elements of the array together in a certain order and outputs it. Therefore, in actual development, we will not often use this method to output arrays.

The following code is an example of using echo to output an array:

<?php
  $arr = array(&#39;苹果&#39;, &#39;香蕉&#39;, &#39;橘子&#39;);
  echo $arr;
?>

Output result:

Array

2. Use print_r to output an array

print_r is provided by PHP A very practical function that can print out the structure information of an array and output it to the screen. Using the print_r function, we can clearly display the structure of the array and help us better understand the elements in the array. The following is an example of using the print_r function to output an array:

<?php
  $arr = array(&#39;苹果&#39;, &#39;香蕉&#39;, &#39;橘子&#39;);
  print_r($arr);
?>

Output results:

Array
(
    [0] => 苹果
    [1] => 香蕉
    [2] => 橘子
)

When outputting, the print_r function will output according to the structure of the array and display the keys and values ​​separately. In this example, we can see that the array has three elements, the key of each element is a number and the value is the corresponding string.

2. Output object

Similar to arrays, objects in PHP are also very common data types. An object is an instantiation of a class and has properties and methods. In PHP, we can use echo or print_r function to output objects.

1. Use echo to output an object

When we use echo to output an object, PHP will convert the object into a string and display it on the screen. However, the output is not intuitive enough because it only shows the memory address of the object.

The following is an example of using echo to output an object:

<?php
  class person {
    public $name = &#39;张三&#39;;
    public $age = &#39;18&#39;;
  }
  $p = new person();
  echo $p;
?>

Output result:

Catchable fatal error: Object of class person could not be converted to string

In this example, we try to use echo to output a person object, but the result is an error message. This is because the echo function does not support direct output objects.

2. Use print_r to output objects

Similar to arrays, we can use the print_r function to output objects. Similarly, when we pass the object to the print_r function for output, PHP will also output the structure information of the object.

The following is an example of using print_r to output an object:

<?php
  class person {
    public $name = &#39;张三&#39;;
    public $age = &#39;18&#39;;
  }
  $p = new person();
  print_r($p);
?>

Output result:

person Object
(
    [name] => 张三
    [age] => 18
)

In this example, we can see that the print_r function outputs according to the structure of the object, And the properties and corresponding values ​​of the object are presented one by one. This can help us understand the structure and properties of objects very well.

3. Conclusion

In PHP, we can use the echo and print_r methods to output arrays and objects. However, since the echo function cannot display the structure of arrays and objects well, we usually use the print_r function for output. Whether it is an array or an object, using the print_r function can clearly display their structural information and help us better understand their elements and attributes.

The above is the detailed content of How to output php array object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn