shavy / s-array
v1.0.3
2014-02-02 18:26 UTC
Requires
- monolog/monolog: ~1.7
- phpunit/phpunit: >3
- shavy/qcache: @dev
This package is not auto-updated.
Last update: 2024-09-24 07:52:38 UTC
README
PHP扩展数组
示例
您可以使用它来向数组列表中添加项s,如下所示。
$array=new SArray;//create object
$array->add("banana","bread"); //add to the array. Think of it like ['banana','bread']
您可以使用它通过循环遍历访问对象。
foreach($array as $a){
echo $a->get($i)
}
如果您想获取一个项的索引,您可以使用get方法并输入类型。
$array->get($i);
>如果您喜欢点语法,它已经为您准备好了,您可以使用它,如下所示。要为某物设置值,您可以这样做。
$array->kv("person.firstname","bill");//neat eh?
$array->kv("person.lastname","bob");
$array->kv("person.age",5);
>这将创建一个看起来像这样的数组
array(
"person"=>array(
"firstname"=>"bill",
"lastname"=>'bob',
"age"=>5
)
);
>您也可以使用点语法像这样获取人员
$array('person.firstname');//returns bill
$array("person.age");//returns 5
//This isn't a typo, you don't need a method name. This will key it very nice and short.
>如果您想存储像文件名这样的没有点语法的东西
$array("filename.ext");
// this will try to get array['filename']['ext']
//want you really want is
$array("filename.ext",false)
>如果您想覆盖或添加新的数组或SArray
//you can use
$array->overwrite(SArray $data) //only takes in a super array
$array->array_overwrite($data) //regular array
// You can tell it exactly what to do. There is no checking in these methods
如果这有点复杂,您可以直接使用replace方法
$array->replace($data);
//Let the program figure it out.
//it will check types
您有标准的toArray方法。这将一直向下转换对象和其他数组。
您还可以使用insert在数组中间插入
$array->insert($data) //you can insert an array for a multi dimension array
$array->insertBatch($data)// you can insert and array of items
/*
$array=SArray::_add("item1","item4");//you can use _ to create the object and add something to it right away
$array->insert(array("item2","item3"),1);
// This will make
/*
array(
"item1",
array(
"item2",
"item3"
),
"item4"
)
but what you really want is
*/
$array=SArray::_add("item1","item4");//you can use _ to create the object and add something to it right away
$array->insertBatch(array("item2","item3"),1);
/*
array(
"item1",
"item2",
"item3"
"item4"
)
more like it
*/
要获取数组的一部分,您可以使用sub($start,$end)。这将返回一个普通数组
$array->sub($start,$end);
$array->sub_as_SArray($start,$end);//they are different methods will likely add shorter version in update
$array->length();// this is not calculated so safe to use in for loops
$array->dump();// var_dump for you
$array->pretty_print();// print_r not it is not the same as print_r($array)
// pretty_print converts to array then prints
//same as
print_r($array->toArray());