Home > Article > Backend Development > How to check the value of php array
PHP array is a very commonly used data type, used to store a set of related values, and can be defined in two ways: index array and associative array. When we use arrays, we often need to view the values in the array. So, how to check the value of PHP array? Let’s discuss it together.
<?php $a = array('apple','banana','orange'); print_r($a); ?>
In the above code, we define an index array $a, which stores three elements, namely 'apple', 'banana ' and 'orange'. Next, use the print_r() function to view the elements in array $a.
Run the above code and the following results will be printed:
Array ( [0] => apple [1] => banana [2] => orange )
Among them, the subscripts of the array elements are enclosed in square brackets [], and the value of the element is displayed behind the arrow. As you can see from the above results, the print_r() function outputs the elements of the array in an easy-to-read format.
It should be noted that the print_r() function does not return the value of the array, it is only used to output the array elements. If you need to process the elements in the array, you need to use other related functions to achieve it.
<?php $a = array('apple','banana','orange'); var_dump($a); ?>
Similar to the print_r() function, the var_dump() function can also be used to view the value of an array. It can output the data type and value of a variable, including arrays, objects, and other types of variables. In the above code, the var_dump() function can print out the following results:
array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(6) "orange" }
As can be seen from the above results, there are three elements in the array $a we defined, and the type of each element is a string. , respectively "apple", "banana", "orange". It can be seen that the var_dump() function is more detailed than the print_r() function, including the data type, length and other information of the element.
In addition to using specific PHP functions to view the values of the array, we can also use the foreach loop statement to traverse an array.
<?php $a = array('apple','banana','orange'); foreach($a as $value){ echo $value."<br>"; } ?>
In the above code, we define an index array $a, use the foreach loop statement to traverse each element in the array, and print the result. Running the above code will print out the following results:
apple banana orange
As can be seen from the above results, using the foreach loop, you can directly access each element in the array, and you can easily view the value of the array.
To sum up, the above three methods can be used to view the value of PHP array. Which method to use needs to be selected according to the specific situation. If you need to view detailed information, you can use the var_dump() function. If you just need to see easy-to-read information, you can use the print_r() function. If you need to traverse and scan array elements, you can use a foreach loop.
The above is the detailed content of How to check the value of php array. For more information, please follow other related articles on the PHP Chinese website!