Home >Backend Development >PHP Problem >php only outputs the key values of the displayed array
In PHP, we often need to output and view the contents of an array. But sometimes we just want to output the keys or values. This article will introduce how to use PHP to output only the key values of an array, and provide some practical application examples.
1. Output only the key values of the array
To achieve only the key values of the array, we need to use some PHP built-in functions and language structures. First, we can use a foreach loop to iterate through the array. But we need to use special syntax:
foreach($array as $key => $value) { echo $key . "<br>"; }
In the above code, we define a foreach loop, the variable $key will store the key name in the current loop, and $value will store its corresponding value. Then we used PHP's built-in echo statement to output the value of the variable $key. At this time, we only output the key name of the array. If we want to output its corresponding value, we only need to change $key in the echo statement to $value.
foreach($array as $key => $value) { echo $value . "<br>"; }
2. Application cases
1. Output the value of a column in the array
Sometimes, the data we process is a two-dimensional array, as shown below:
$array = array( array('id' => 1, 'name' => 'Tom', 'age' => 20), array('id' => 2, 'name' => 'Jerry', 'age' => 24), array('id' => 3, 'name' => 'Emily', 'age' => 22) );
If we only need to output a certain column, such as the 'name' column, we can use the following code:
foreach($array as $key => $value) { echo $value['name'] . "<br>"; }
In the above code, we use a key name similar to a two-dimensional array. Access the 'name' attribute of each element and print its value. In this way, we only output the contents of the ‘name’ column.
2. Query the array elements that meet the conditions
Sometimes, we need to query the elements that meet certain conditions from the array, and then only output a certain column among them. For example, if we need to query the names of students older than 20 years old, we can use the following code:
foreach($array as $key => $value) { if($value['age'] > 20) { echo $value['name'] . "<br>"; } }
In the above code, we added an if statement to filter elements that meet the conditions when looping through the array. If the age attribute of the current element is greater than 20, we output its name attribute.
3. Dynamically construct HTML list
In PHP, we can use arrays to dynamically construct HTML lists. For example, we have an array that stores multiple links and their URL addresses:
$links = array( "Google" => "http://www.google.com", "Baidu" => "http://www.baidu.com", "Facebook" => "http://www.facebook.com", );
If we want to construct these links into an HTML list, we can use the following code:
echo "<ul>"; foreach($links as $key => $value) { echo "<li><a href='$value'>$key</a></li>"; } echo "</ul>";
In the above code, we use a foreach loop to traverse the array, and use the key name of each element as the link text and the key value as the link address. We then wrap this data into a list using HTML tags.
4. Check whether an element exists in the array
Sometimes, we need to check whether an element exists in the array. PHP provides the in_array() function to quickly complete this task. For example, we have an array of numbers:
$numbers = array(1, 2, 3, 4, 5);
If we want to check whether the number 3 exists in the array, we can use the following code:
if(in_array(3, $numbers)) { echo "数字3存在于数组中"; } else { echo "数字3不存在于数组中"; }
In the above code, we use in_array() Function to check whether the number 3 exists in the array $numbers. If it exists, we will output 'Number 3 exists in array', otherwise we will output 'Number 3 does not exist in array'.
Summary
This article introduces how to output only the key values of an array in PHP, and provides some practical application cases. In actual development, we will encounter more array-related operations, and flexibly mastering array processing skills can greatly improve the efficiency of the code.
The above is the detailed content of php only outputs the key values of the displayed array. For more information, please follow other related articles on the PHP Chinese website!