When using PHP to do a project, you need to use a data structure to store data, but in PHP, it seems that it can only be stored using arrays.
The configuration files of PHP's Laravel and other frameworks all use multi-dimensional arrays, such as:
// app/config/database.php 文件
return array(
/*
|--------------------------------------------------------------------------
| PDO 类型
|--------------------------------------------------------------------------
| 默认情况下 Laravel 的数据库是用 PDO 来操作的,这样能极大化的提高数据库兼容性。
| 那么默认查询返回的类型是一个对象,也就是如下的默认设置。
| 如果你需要返回的是一个数组,你可以设置成 'PDO::FETCH_ASSOC'
*/
'fetch' => PDO::FETCH_CLASS,
/*
|--------------------------------------------------------------------------
| 默认的数据库连接名
|--------------------------------------------------------------------------
| 这里所说的名字是和下面的 'connections' 中的名称对应的,而不是指你用的什么数据库
| 为了你更好的理解,我在这里换了一个名字
*/
'default' => 'meinv',
/*
|--------------------------------------------------------------------------
| 数据库连接名
|--------------------------------------------------------------------------
| 这里就是设置各种数据库的配置的,每个数组里的 'driver' 表明了你要用的数据库类型
| 同一种数据库类型可以设置多种配置,名字区分开就行,就像下面的 'mysql' 和 'meinv'
| 其他的么,我觉得不需要解释了吧,就是字面意思,我相信你英文的能力(其实是我英文不好)
*/
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'meinv' => array( //这里就是上面例子里的默认连接数据库名,实际上是 mysql 数据库
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
);
I think the efficiency of multi-dimensional arrays will be very low, but the Laravel framework is used in this way, so I don't know whether it is feasible to use multi-dimensional arrays in PHP. I hope you can answer it! ! ! Thank you! ! !
There are only arrays in PHP, you have no choice. Laravel is a framework based on PHP, so its syntax cannot be separated from PHP;
Efficiency depends on how you use it. Of course, multi-layer for loop operations will be inefficient, but if you use more indexes, the efficiency will be okay
There is no such problem as low efficiency. . PHP's array implementation is originally a doubly linked list. .
This is not an efficiency issue. The efficiency of database query is more important
Execution efficiency does not depend on the running speed of php, IO is the bottleneck. This thing can be processed by PHP at MS level
Laravel: I won’t bear this responsibility!
The configuration files of most PHP frameworks and programs are like this. If the efficiency really drops, the Laravel framework, as the leader of PHP, will definitely use a better way to handle it.
In addition, PHP7 is much more efficient in array operations than PHP5.
Are there many projects that use JSON to save configurations?
The associative array in PHP is equivalent to the JSON object in JS.
So you can compare the performance of PHP's associative array with the JSON object of Node.JS.
Generate a file containing Associative array (mapping/dictionary) of 1 million elements:
PHP7 takes only 1/3 of Node7, and even PHP5 is faster than Node7.
It can be seen that the performance of PHP associative array is quite good .
First of all, let me mention that it is nonsense to talk about efficiency without the usage scenario. If you need development efficiency, use
php
, and if you need operating efficiency, usec/c++
.Answer the question: It is very feasible when the website traffic is not large.