mmamedov/array-property 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mmamedov/array-property

Composer 安装命令:

composer require mmamedov/array-property

包简介

Access arrays in an intuitive and object-oriented way. Keys and values are treated as properties.

README 文档

README

Build Status Latest Stable Version License

ArrayProperty

Syntax sugar for working with arrays in PHP. Read and write to arrays in an object-oriented style. Keys are treated as properties.

ArrayProperty can be used on any PHP array variable.

Install using composer (recommended):

composer require mmamedov/array-property

Or manually add to your composer.json file:

"require": {
    "mmamedov/array-property": "^1.1"
}

Usage

Consider the following sample PHP array:

$sample =
    array(
        'app' => array(
            'log_dir' => '/log/path',
            'debug' => false,
            'log' => true,
            'version' => '2.3',
            'deep' => array(
                'inner' => 'some value',
                'level' => '2'
            )
        ),
        'my node' => 'some value'
    );

This is how easy to work with it using ArrayProperty:

$a = new ArrayProperty($sample);
echo $a->app->log_dir;      //outputs /log/path
echo $a->app->deep->inner;  //outputs 'some value'

//convert to array
$deep = $a->app->deep->toArray();
print_r($deep);             //outputs deep as array;

//check if value exists:
$a->app->exist('log_dir')   //returns true

Load new array and acces its values inline:

$prop = new ArrayProperty(array());
$new = array(1 => 'apple', 2 => 'orange', 3 => 'olive', 4 => 'grapes', 'multi'=>array('key'=>'value'));
echo $prop->loadArray($new)->{1}; //outputs "apple"
echo $prop->multi->key;           //outputs "value"

Now let's add new element to the $prop ArrayProperty object we created above:

$prop->myNewNode = "myValue";   //you can assign arrays as well.
echo $prop->myNewNode;          //outputs 'myValue';
//overwrite existing elements
$prop->{1} = "banana"; 
echo $prop->{1};                //now outputs "banana" instead of "apple"

Please note that multidimensional write is not supported, like in $prop->firstIndex->second = "value";

You can switch modes between OBJECT_MODE and MIXED_MODE(default). Mode is set as second parameter in the constructor, or by calling setMode() method. In MIXED_MODE array values are returned in their original type, whereas in OBJECT_MODE returned values are always ArrayProperty object:

$prop->setMode(ArrayProperty::OBJECT_MODE);
var_dump($prop->myNewNode);     //returns ArrayProperty object

$prop->setMode(ArrayProperty::MIXED_MODE);
var_dump($prop->myNewNode);     //returns "myValue" which was set before

For more examples see code inside ArrayProperty examples directory.

Enjoy!

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固