If PHP uses multi-dimensional arrays, will the efficiency be very low?
PHP中文网
PHP中文网 2017-06-15 09:22:11
0
7
885

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! ! !

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(7)
为情所困
  1. 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;

  2. 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, use c/c++.

Answer the question: It is very feasible when the website traffic is not large.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template