Ich versuche, eine Funktion in PHP erstellen, die aus einem Array lesen wird. Ich versuche, Text und die Array-ID-Nummer zu verketten, um den Wert im Array zu lesen, jedoch wird der verkettete Text zurückgegeben, nicht der Wert des Arrays.verketten Textwerte auf Array-Element in PHP zugreifen
Hier ist mein Code:
//arrays with words in dictionary
$dictionary_word1 = array("test1","test2","test3");
$dictionary_word2 = array("test4","test5","test6");
$dictionary_word3 = array("test7","test8","test9");
$word_to_lookup = "dictionary_word_1";
//value to send to function
$returned_word = convert_word($word_to_lookup);
//value returned from function
echo "<br>the returned word from the function is " . $returned_word;
//the text "$dictionary_word_1[2]" is displayed instead of array value
echo "<br>the value in the array is " . $dictionary_word1[2];
//this displays correcty as "test3"
function convert_word($word_to_convert)
{
global $dictionary_word1;
$converted_word = '$'.$word_to_convert .'[2]';
return $converted_word;
}
Könnte jemand mir irgendwelche Tipps, wo ich falsch gehe?
'$ converted_word = $ {$ word_to_convert} [2];' Aber es gibt bessere Möglichkeiten, zu tun, was auch immer Sie tun. – AbraCadaver