zihad/easyarray 问题修复 & 功能扩展

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

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

zihad/easyarray

最新稳定版本:1.0.1

Composer 安装命令:

composer require zihad/easyarray

包简介

A simple php package to handle your array easily and elegantly

README 文档

README

This package will allow developers to interact with complex and nested arrays easily. Any array can be converted into an EasyArray object and then access the array items like object properties.

Installation:

composer require zihad/easyarray

Usage

Basics

use Zihad\Easyarray\EasyArray;

$testArray = [
    'name' => 'Zihad',
    'stack' => 'LAMP'
];
$easyArray = new EasyArray($testArray);

// Access array items
$easyArray->name // 'Zihad'
$easyArray['stack'] // 'LAMP'
$easyArray->language // null

// Check if the value is set
isset($easyArray->name) // true
isset($easyArray['stack']) // true
isset($easyArray['lang']) // false

// Unset value
unset($easyArray['name']);
unset($easyArray->name);
isset($easyArray->name) // false
$easyArray->name // null

// Iterating throught the array
foreach($easyArray as $key => $value) {
    echo "$key -> $value\n";
}
// Output
name -> Zihad
stack -> LAMP

// Working with nested array
$testArray = [
    'name' => 'Zihad',
    'stack' => [
        'lang' => 'PHP',
        'framework' => 'Laravel'
    ]
];
$easyArray = new EasyArray($testArray, true); // secord parameter is a flag to indicate if it is a nested array or not

$easyArray->stack->framework // 'Laravel'
isset($easyArray->stack->lang) // true


$easyArray->toArray()
//Output
[
    'name' => 'Zihad',
    'stack' => [
        'lang' => 'PHP',
        'framework' => 'Laravel'
    ]
]

$easyArray->stack->toArray()
// Output
[
    'lang' => 'PHP',
    'framework' => 'Laravel'
]


// You can also use helper function to create a new instance of EasyArray
$easyArray = easyArray([
    'name' => 'Zihad',
    'stack' => [
        'lang' => 'PHP',
        'framework' => 'Laravel'
    ]
]);

Iterations

// You can use EasyArray in foreach and nested foreach loops
$easyArray = easyArray([
    'phones' => [
        'featured' => [
            'used' => [
                'Nokia 3310',
                'Oppo'
            ],
            'fresh' => [
                'Nokia 1110',
                'Huawei 11'
            ]
        ],
        'smartphone' => [
            'used' => [
                'iPhone 11',
                'Samsung s20'
            ],
            'fresh' => [
                'iPhone 13 pro',
                'One plus 9'
            ]
        ]
    ]
], true);

foreach ($easyArray as $category) {
    foreach ($category as $name => $subCategory) {
        foreach ($subCategory as $index => $phones) {
            var_dump(sprintf(
                "%s > %s phones: %s",
                ucfirst($name),
                ucfirst($index),
                implode(', ', $phones->toArray())
            ));
        }
    }
}

Array operations

// Count the total number of elements
$easyArray = easyArray([
    'used' => [
        'iPhone 11',
        'Samsung s20'
    ],
    'fresh' => [
        'iPhone 13 pro',
        'One plus 9'
    ]
]);
count($easyArray) // 2
$easyArray->count() // 2


// Array push() method
$easyArray = easyArray([
    'used' => [
        'iPhone 11',
        'Samsung s20'
    ],
    'fresh' => [
        'iPhone 13 pro',
        'One plus 9'
    ]
], true);

$easyArray->used->push('Pixel 5');
$easyArray->used->count() // 3


// Array pop() method
$easyArray = easyArray([
    'used' => [
        'iPhone 11',
        'Samsung s20'
    ]
]);

$easyArray->merge([
    'fresh' => [
        'iPhone 13 pro',
        'One plus 9'
    ]
]);

$easyArray->fresh == null; // false
$easyArray->pop();
$easyArray->fresh == null; // true

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-20