该库不在主动发展中。其他各种馆藏库提供了我们需要的大多数功能。
错误仅修复。
ArrayObject是一个PHP库,提供了一个轻巧的接口,用于流利地使用数组。
ArrayObject提供了围绕PHP内置array类型的包装器,其中包括用于过滤和检索项目的方法,并方便地将单个项目和集合视为相同的对象。
这对于使用API请求的JSON响应特别有用。
超级HTTP客户端使用此库来使从响应超级直观的响应中提取数据。
composer require rexlabs/array-object <?php
require ' vendor/autoload.php ' ;
use Rexlabs ArrayObject ArrayObject ;
// Initialise from an Array
$ obj = ArrayObject:: fromArray ([...]);
// Initialise from a JSON encoded string
$ obj = ArrayObject:: fromJson ( $ str );
// Output to array
$ arr = $ obj -> toArray ();
// Output to JSON encoded string
$ json = $ obj -> toJson ();下面的示例基于以下输入数据。
{
"books" : [
{
"id" : 1 ,
"title" : " 1984 " ,
"author" : " George Orwell "
},
{
"id" : 2 ,
"title" : " Pride and Prejudice " ,
"author" : " Jane Austen "
}
]
} $ obj -> books ; // Instance of ArrayObject
$ obj -> books -> pluckArray ( ' author ' ); // array [ 'George Orwell', 'Jane Austen' ]
$ obj -> pluckArray ( ' books.author ' ); // array [ 'George Orwell', 'Jane Austen' ]
$ obj -> books -> count (); // 2
$ obj -> books -> isCollection (); // true
$ obj -> books [ 0 ]; // Instance of ArrayObject
$ obj -> books [ 0 ]-> isCollection (); // false
$ obj -> books [ 0 ]-> id ; // 1注意:所有方法都优雅地处理单个项目作为数组。
get()方法允许您获取属性的值,并在不存在该值时接收默认值。它还支持深度. :
$ obj -> get ( ' books.0 ' ); // ArrayObject
$ obj -> get ( ' books.0.title ' ); // "1984"
$ obj -> get ( ' books.1.title ' ); // "Pride and Prejudice"
$ obj -> get ( ' books.0.missing ' , ' Default value ' ); // "Default value"您还可以使用对象属性语法(我们过载__get()实现此目的)来获取属性:
$ obj -> books [ 0 ]; // ArrayObject
$ obj -> books [ 0 ]-> title ; // "1984"
$ obj -> books [ 1 ]-> title ; // "Pride and Prejudice"
$ obj -> books [ 0 ]-> missing ; // throws InvalidPropertyException 当通过get('some.key')是一个数组时,将其装在ArrayObject的实例中。但是, getRaw()方法在返回的值上没有执行任何“拳击”。
$ obj -> get ( ' books ' ); // ArrayObject
$ obj -> getRaw ( ' books ' ); // (array)
$ obj -> get ( ' books.0.title ' ); // "1984"
$ obj -> getRaw ( ' books.0.title ' ); // "1984" set()方法允许您使用点符号设置属性。它将自动创建基础数组结构:
$ obj -> set ( ' some.deep.key ' , $ value ); // Set nested property
$ obj -> set ( ' some_key ' , $ anotherArrayObject ); // Pass an ArrayObjectInterface as value当$onlyIfExists标志以true方式传递时,只有在密钥已经存在的情况下,它才会设置该属性。
类似于get() ,但是当找不到键时,会抛出InvalidPropertyException 。
要测试存在元素的存在,请使用has()方法:
$ obj -> has ( ' books ' ); // true
$ obj -> has ( ' books.0.id ' ); // true
$ obj -> has ( ' books.1.name ' ); // false
$ obj -> has ( ' books.5 ' ); // false 通过将回调传递给each() :
$ obj -> books -> each ( function ( $ book ) {
echo " ID: { $ book -> id } , title: { $ book -> title }n" ;
});
foreach ( $ obj -> books as $ book ) {
echo " ID: { $ book -> id } , title: { $ book -> title }n" ;
}如果您有一个项目,它仍然有效。
由于ArrayObject实现了迭代界面,因此您可以简单地在对象上foreach() 。这适用于单个项目或收藏。
foreach ( $ obj -> books as $ book ) {
echo " ID: { $ book -> id } , title: { $ book -> title }n" ;
}返回一个包含拔出财产的新系列。
$ titles = $ obj -> books -> pluck ( ' title ' ); // ArrayObject
$ arr = $ titles -> toArray (); // ['1984', 'Pride and Prejudice'] 返回一系列新的摘要财产。这提供了pluck($key)->toArray()的快捷方式:
$ arr = $ obj -> books -> pluckArray ( ' title ' ); // ['1984', 'Pride and Prejudice'] 您可以拨打计数任何节点:
$ obj -> count (); // 1
$ obj -> books -> count (); // 2
$ obj -> books [ 0 ]-> count (); // 1注意:当单个项目(即,不是集合)调用时,它将返回1。
当收集至少包含一个项目时,返回true。
$ obj -> hasItems ();注意:当单个项目(即。不是集合)时,它将返回true
应用回调或“在哪里”条件的数组,仅返回匹配的项目。
使用回调,每个项目都是ArrayObject的实例:
// Only return items with a title
$ filteredBooksWithTitle = $ obj -> books -> filter ( function ( $ book ) {
return $ book -> has ( ' title ' );
});您还可以使用数组指定条件列表:
// Only return items with a title
$ filteredBooksWithTitle = $ obj -> books -> filter ([ " title " => ' 1984 ' ]);获取原始数组可以通过toArray()方法提供:
$ obj -> books [ 0 ]-> toArray (); // [ 'id' => 1, 'title' => '1984', 'author' => 'George Orwell' ]
$ obj -> get ( 1 )-> toArray (); // [ 'id' => 2, 'title' => 'Pride and Prejudice', 'author' => 'Jane Austen' ]
$ obj -> books -> toArray (); // [ [ 'id' => 1, 'title' => '1984', 'author' => 'George Orwell' ], [ 'id' => 2, 'title' => 'Pride and Prejudice', 'author' => 'Jane Austen' ] ]
$ obj -> toArray (); // [ 'books' => [ [ 'id' => 1, 'title' => '1984', 'author' => 'George Orwell' ], [ 'id' => 2, 'title' => 'Pride and Prejudice', 'author' => 'Jane Austen' ] ] ] 返回基础数组的JSON编码字符串:
$ json = $ obj -> toJson (); // '{ "some_prop": [ "val1", "val2" ] }` 确定基础阵列是否是集合。
$ obj -> books -> isCollection (); // true
$ obj -> get ( ' books.0 ' )-> isCollection (); // false
$ obj -> isCollection (); // false 在集合的开始中添加一个或多个项目。如果数组当前不是集合,它将转换为带有一个元素的集合。
$ obj -> unshift ( ' value ' )
-> unshift ( ' value1 ' , ' value2 ' , ' value3 ' ); 注意:您可以将ArrayObject作为值传递。
从集合中摘下第一项。
如果数组当前不是集合,它将转换为带有一个元素的集合。
$ item = $ obj -> shift (); // ArrayObject 在集合的末尾添加一个或多个项目。如果数组当前不是集合,它将转换为带有一个元素的集合。
$ obj -> push ( ' value ' )
-> push ( ' value1 ' , ' value2 ' , ' value3 ' ); 注意:您可以将ArrayObject作为值传递。
从集合中删除最后一项。
如果数组当前不是集合,它将转换为带有一个元素的集合。
$ item = $ obj -> pop (); // ArrayObject 欢迎捐款,请提交抽拉赛或创建问题。您提交的代码应使用PSR-1/PSR-2标准进行格式。