What is the method for setting up an associative array in PHP?

In PHP, you can use the following method to set elements in an associative array:

// 创建一个空的关联数组
$fruit = array();

// 设置关联数组元素
$fruit['apple'] = 'red';
$fruit['banana'] = 'yellow';
$fruit['orange'] = 'orange';

// 使用数组字面量的方式设置关联数组
$color = array(
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
);

// 使用 array() 函数的方式设置关联数组
$color = array(
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
);

// 使用数组赋值方式设置关联数组
$fruit = [
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
];

The above are several common methods for setting associative array elements.

bannerAds