How can keys be utilized as an index in the $convertedValues array in PHP for unit conversion?
To utilize keys as an index in the $convertedValues array in PHP for unit conversion, you can set the keys of the array to represent the units being converted. This way, you can easily access the converted values by referencing the specific unit key. By using keys as indexes, you can organize and retrieve the converted values more efficiently.
// Define the conversion rates for different units
$conversionRates = [
'kg_to_lb' => 2.20462,
'm_to_ft' => 3.28084,
// Add more conversion rates as needed
];
// Convert values based on the conversion rates
$convertedValues = [
'kg_to_lb' => 5 * $conversionRates['kg_to_lb'],
'm_to_ft' => 10 * $conversionRates['m_to_ft'],
// Add more converted values as needed
];
// Access the converted values using keys as indexes
echo "5 kg is equal to " . $convertedValues['kg_to_lb'] . " lbs\n";
echo "10 meters is equal to " . $convertedValues['m_to_ft'] . " feet\n";
Related questions
-
SQL query PHP code array data
-
debug_backtrace instantiation class
-
CSV API PHP