Home > Backend Development > PHP Problem > How to check the value of php array

How to check the value of php array

PHPz
Release: 2023-04-20 14:57:57
Original
716 people have browsed it

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.

1. Use the print_r() function

<?php
    $a = array(&#39;apple&#39;,&#39;banana&#39;,&#39;orange&#39;);
    print_r($a);
?>
Copy after login

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
)
Copy after login

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.

2. Use the var_dump() function

<?php
    $a = array(&#39;apple&#39;,&#39;banana&#39;,&#39;orange&#39;);
    var_dump($a);
?>
Copy after login

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"
}
Copy after login

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.

3. Use foreach() loop traversal

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(&#39;apple&#39;,&#39;banana&#39;,&#39;orange&#39;);
    foreach($a as $value){
        echo $value."<br>";
    }
?>
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template