How can basic array manipulation be used to extract specific data from a fetched array in PHP?
When working with fetched arrays in PHP, basic array manipulation can be used to extract specific data by accessing the array elements using their keys or indexes. This can be done by using functions like `array_column()` to extract specific columns from a multidimensional array or using loops like `foreach` to iterate through the array and extract the desired data.
// Example code to extract specific data from a fetched array in PHP
// Assuming $fetchedArray is the fetched array containing data
$fetchedArray = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Alice', 'age' => 28]
];
// Extracting names from the fetched array
$names = array_column($fetchedArray, 'name');
print_r($names);
// Extracting names and ages from the fetched array
foreach ($fetchedArray as $data) {
echo $data['name'] . ' is ' . $data['age'] . ' years old.<br>';
}
Related Questions
- What are the best practices for creating a secure and accurate database backup in PHP without using addslashes() for JSON strings?
- What are the advantages of using PEAR::QuickForm for handling form data in PHP applications?
- What strategies can be employed to ensure active or highlighted states in navigation elements when using PHP for menu generation?