溫馨提示×

php關(guān)聯(lián)數(shù)組如何輸出

PHP
小億
85
2024-02-21 10:31:19
欄目: 編程語言

關(guān)聯(lián)數(shù)組可以通過foreach循環(huán)遍歷輸出,也可以通過鍵值來訪問特定的值。以下是一些例子:

  1. 使用foreach循環(huán)輸出關(guān)聯(lián)數(shù)組的所有值:
$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");

foreach($fruits as $key => $value) {
    echo "The color of $key is $value <br>";
}

輸出結(jié)果:

The color of apple is red
The color of banana is yellow
The color of orange is orange
  1. 使用鍵值來訪問特定的值:
$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");

echo "The color of apple is " . $fruits["apple"];

輸出結(jié)果:

The color of apple is red

通過以上方法,可以方便地輸出關(guān)聯(lián)數(shù)組的值。

0