ThinkPHP is a PHP development framework based on the MVC pattern, which has become the framework of choice for many developers. When developing applications, we usually need to use arrays to store and manipulate data. This article will introduce how to use arrays in the ThinkPHP framework.
In ThinkPHP development, we can use arrays to store various types of data. The method of defining an array is the same as the PHP native language syntax, for example:
$arr = array('a', 'b', 'c', 'd');
You can also use short syntax to define an array, for example:
$arr = ['a', 'b', 'c', 'd'];
In the ThinkPHP framework, we usually use C functions to define and initialize the array. For example:
$arr = C('config');
Where, 'config' is the key name of a configuration file. You can obtain the array in the configuration file by calling the C function and passing the key name as a parameter. In addition, we can also use the config function to obtain configuration information, for example:
$arr = config('database');
At this time, $arr will be initialized as an array of database configuration information.
In the ThinkPHP framework, we usually use $data name['key name'] or $data name.key name to access array elements . For example:
$arr = ['a', 'b', 'c', 'd']; echo $arr[0]; // 输出a echo $arr[3]; // 输出d $config = C('config'); echo $config['DB_HOST']; // 输出数据库主机地址
It should be noted that if the key name does not exist in the array, a Notice level error message will be generated.
In the ThinkPHP framework, we can use $array name['key name'] or $array name.key name to modify The value of the array element. For example:
$arr = ['a', 'b', 'c', 'd']; $arr[1] = 'e'; $arr['2'] = 'f'; // 注意键名的引号 print_r($arr); // 输出Array ( [0] => a [1] => e [2] => f [3] => d )
When deleting array elements, we can use the unset function to achieve this. For example:
$arr = ['a', 'b', 'c', 'd']; unset($arr[2]); print_r($arr); // 输出Array ( [0] => a [1] => b [3] => d )
It should be noted that the key name of the array will be re-indexed after using the unset function.
In the ThinkPHP framework, we can use the foreach loop to traverse the array. For example:
$arr = ['a', 'b', 'c', 'd']; foreach ($arr as $value) { echo $value . " "; } // 输出a b c d
When traversing an associative array (that is, an array containing key names), we need to use the key-value syntax of foreach, for example:
$config = C('config'); foreach ($config as $key => $value) { echo $key . ":" . $value . "<br>"; }
When traversing a multi-dimensional array, we can Use multiple foreach statements. For example:
$arr = [['a', 'b'], ['c', 'd']]; foreach ($arr as $value1) { foreach ($value1 as $value2) { echo $value2 . " "; } }
After the above introduction, we can easily define, initialize, access, modify, delete and traverse arrays in the ThinkPHP framework. Hope this article is helpful to you.
The above is the detailed content of Let's talk about how to use arrays in the ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!