PHPackages                             lappcloud/php-code-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lappcloud/php-code-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lappcloud/php-code-generator
============================

PHP代码生成

0.1.1(6y ago)17MITPHPPHP &gt;=5.4

Since Aug 4Pushed 6y ago1 watchersCompare

[ Source](https://github.com/lappcloud/php-code-generator)[ Packagist](https://packagist.org/packages/lappcloud/php-code-generator)[ RSS](/packages/lappcloud-php-code-generator/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (3)DependenciesVersions (5)Used By (0)

PHP常量代码生成
=========

[](#php常量代码生成)

遇到的问题：
------

[](#遇到的问题)

PM给了一个包含几百条记录的客户和客户代码的excel表，需要将其转成一个PHP的Map数组。一条一条复制很麻烦，所以有了这个库。

输入数据：json，excel文本

输出数据：
-----

[](#输出数据)

PHP二维数组代码 等同于 json的对象数组(第一行作为key)

PHP普通数组代码（只取某一列组成数组，默认第一列）

PHP关联数组代码（只取其中某两列，一列作为key，一列作为value）

json对象数组转php数组(原样输出)

json对象数组转php数组(通过模板过滤)

json字符串美化（可以为每行设置输出前缀）

开发思路
----

[](#开发思路)

[![Image text](https://raw.githubusercontent.com/lappcloud/php-code-generator/master/doc/design.png)](https://raw.githubusercontent.com/lappcloud/php-code-generator/master/doc/design.png)

安装
--

[](#安装)

推荐通过composer扩展的方式进行安装

```
composer require lappcloud/php-code-generator

```

或者

```
"lappcloud/php-code-generator": "*"

```

独立使用
----

[](#独立使用)

```
git clone https://github.com/lappcloud/php-code-generator.git && cd php-code-generator && composer update

用待处理字符串特换掉$string，然后执行对应的php文件，如：

php test/1.字符串生成对象数组.php

```

使用示例
----

[](#使用示例)

#### 1.生成对象数组，json的概念，这里用起来比较形象（每行一个对象，取第一行作为key）

[](#1生成对象数组json的概念这里用起来比较形象每行一个对象取第一行作为key)

```
   use lappcloud\codegen\GenPHPCode;

   // 生成对象数组，json的概念，这里用起来比较形象（每行一个对象，取第一行作为key）
   $string = intFormatType = GenPHPCode::FORMAT_TYPE_OBJECT_ARRAY;

       $res = $obj->str2Code($string);
       //$res = $obj->str2Code($json);
   } catch (\Exception $e) {
       echo 'Error:' . $e->getMessage() . "\n"; exit;
   }

   ob_clean();
   echo $res;

   // 输出：
   [
       [
           'id'   => '1',
           'name' => '张三',
           'sex'  => 'man',
           'age'  => '18',
       ],
       [
           'id'   => '2',
           'name' => '李四',
           'sex'  => 'man',
           'age'  => '17',
       ],
   ];

```

#### 2.生成普通数组（只取某一列组成数组，默认第一列）

[](#2生成普通数组只取某一列组成数组默认第一列)

```
include 'vendor/autoload.php';

// 生成普通数组（只取某一列组成数组，默认第一列）
$string = intFormatType = GenPHPCode::FORMAT_TYPE_ORDINARY_ARRAY;

    $res = $obj->str2Code($json);
} catch (\Exception $e) {
    echo 'Error:' . $e->getMessage() . "\n"; exit;
}

ob_clean();
echo $res;

// 输出：
[
    '张三',
    '李四',
];

```

#### 3.取excel的第2列数据作为key，第3列作为value，第三列数据作为value生成关联数组

[](#3取excel的第2列数据作为key第3列作为value第三列数据作为value生成关联数组)

```
  include 'vendor/autoload.php';

  // 取excel的第2列数据作为key，第三列数据作为value生成关联数组

  $string = intFormatType = GenPHPCode::FORMAT_TYPE_LIST_ARRAY;

      $res = $obj->str2Code($string);
  } catch (\Exception $e) {
      echo 'Error:' . $e->getMessage() . "\n"; exit;
  }

  ob_clean();
  echo $res;

  // 输出：
  [
      'Q010Y' => '客户A',
      'Q011Y' => '客户B',
  ];

```

#### 4.json对象数组转php数组(原样输出)

[](#4json对象数组转php数组原样输出)

```
include 'vendor/autoload.php';

use lappcloud\codegen\GenPHPCode;

$json = str2Code($json);
} catch (\Exception $e) {
    echo 'Error:' . $e->getMessage() . "\n"; exit;
}

ob_clean();
echo $res;

// 输出：
[
    [
        'name' => 'Google',
        'url'  => [
            '0'     => 'value',
            '1'     => 'value',
            '2'     => 'value',
            'field' => 'value',
        ],
        'tag'  => [
            'search',
            'tag1',
        ],
    ],
    [
        'name' => 'Baidu',
        'url'  => 'http://xxx',
    ],
];

```

#### 5.json对象数组转php数组(通过模板过滤)

[](#5json对象数组转php数组通过模板过滤)

```
include 'vendor/autoload.php';

use lappcloud\codegen\GenPHPCode;

$json =  true,
        'field' => true,
    ],
    'tag'  => true,
];

try {
    $obj = new GenPHPCode();
    $obj->intInputType = GenPHPCode::INPUT_TYPE_JSON;
    $obj->intFormatType = GenPHPCode::FORMAT_TYPE_OBJECT_TEMPLATE;
    $obj->template = $template;

    $res = $obj->str2Code($json);
} catch (\Exception $e) {
    echo 'Error:' . $e->getMessage() . "\n"; exit;
}

ob_clean();
echo $res;

// 输出：
[
    [
        'name' => 'Google',
        'url'  => [
            '0'     => 'value',
            'field' => 'value',
        ],
        'tag'  => [
            'search',
            'tag1',
        ],
    ],
    [
        'name' => 'Baidu',
        'url'  => [
            '0'     => [
            ],
            'field' => [
            ],
        ],
        'tag'  => [
        ],
    ],
];

```

#### 6.json对象数组转php数组(通过模板校验)

[](#6json对象数组转php数组通过模板校验)

```
include 'vendor/autoload.php';

use lappcloud\codegen\format\FormatByTemplate;

$json =  true,
        'field' => true,
    ],
    'tag'  => true,
];

print_r(FormatByTemplate::validate(json_decode($json, true), $template));

// 输出：

//Array
//(
//    [0] => Array
//        (
//            [error_data] => Array
//                (
//                    [name] => Baidu
//                    [url] => http://xxx
//                )
//
//            [error_msg] => url是字符串类型
//        )
//
//)

```

License
-------

[](#license)

MIT

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~82 days

Total

4

Last Release

2228d ago

PHP version history (2 changes)v0.0.1PHP ^5.4

v0.0.2PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/05e66578ef7ea8031205a379698c7179fa56383a38ee80db49f46ba021e1d713?d=identicon)[lappcloud](/maintainers/lappcloud)

---

Top Contributors

[![lappcloud](https://avatars.githubusercontent.com/u/53545660?v=4)](https://github.com/lappcloud "lappcloud (13 commits)")

### Embed Badge

![Health badge](/badges/lappcloud-php-code-generator/health.svg)

```
[![Health](https://phpackages.com/badges/lappcloud-php-code-generator/health.svg)](https://phpackages.com/packages/lappcloud-php-code-generator)
```

###  Alternatives

[bentools/cartesian-product

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

87905.6k18](/packages/bentools-cartesian-product)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
