Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
6d917566
Commit
6d917566
authored
Apr 12, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added more methods to ArrayHelper.
parent
c4d11c6a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
7 deletions
+99
-7
ArrayHelper.php
framework/util/ArrayHelper.php
+99
-7
No files found.
framework/util/ArrayHelper.php
View file @
6d917566
...
...
@@ -71,8 +71,14 @@ class ArrayHelper extends \yii\base\Component
}
/**
* Converts an array such that it is indexed by the specified column values or anonymous function results.
* This method should be used only for two or higher dimensional arrays.
* Indexes an array according to a specified key.
* The input array should be multidimensional or an array of objects.
*
* The key can be a key name of the sub-array, a property name of object, or an anonymous
* function which returns the key value given an array element.
*
* If a key value is null, the corresponding array element will be discarded and not put in the result.
*
* For example,
*
* ~~~
...
...
@@ -84,18 +90,17 @@ class ArrayHelper extends \yii\base\Component
* // the result is:
* // array(
* // '123' => array('id' => '123', 'data' => 'abc'),
* // '345' => array('id' => '
123', 'data' => 'abc
'),
* // '345' => array('id' => '
345', 'data' => 'def
'),
* // )
*
* // using anonymous function
* $result = ArrayHelper::index($array, function(element) {
* return $element['id'];
* });
* ~~~
*
* Note that if an index value is null, it will NOT be added to the resulting array.
*
* @param array $array the multidimensional array that needs to be indexed
* @param mixed $key the column name or anonymous function whose result will be used to index the array
* @param array $array the array that needs to be indexed
* @param string|\Closure $key the column name or anonymous function whose result will be used to index the array
* @return array the indexed array (the input array will be kept intact.)
*/
public
static
function
index
(
$array
,
$key
)
...
...
@@ -110,11 +115,97 @@ class ArrayHelper extends \yii\base\Component
}
}
else
{
foreach
(
$array
as
$element
)
{
if
(
is_object
(
$element
))
{
if
((
$value
=
$element
->
$key
)
!==
null
)
{
$result
[
$value
]
=
$element
;
}
}
elseif
(
is_array
(
$element
))
{
if
(
isset
(
$element
[
$key
])
&&
$element
[
$key
]
!==
null
)
{
$result
[
$element
[
$key
]]
=
$element
;
}
}
}
}
return
$result
;
}
/**
* Returns the values of a specified column in an array.
* The input array should be multidimensional or an array of objects.
*
* For example,
*
* ~~~
* $array = array(
* array('id' => '123', 'data' => 'abc'),
* array('id' => '345', 'data' => 'def'),
* );
* $result = ArrayHelper::column($array, 'id');
* // the result is: array( '123', '345')
*
* // using anonymous function
* $result = ArrayHelper::column($array, function(element) {
* return $element['id'];
* });
* ~~~
*
* @param array $array
* @param string|\Closure $key
* @return array the list of column values
*/
public
static
function
column
(
$array
,
$key
)
{
$result
=
array
();
if
(
$key
instanceof
\Closure
)
{
foreach
(
$array
as
$element
)
{
$result
[]
=
call_user_func
(
$key
,
$element
);
}
}
else
{
foreach
(
$array
as
$element
)
{
if
(
is_object
(
$element
))
{
$result
[]
=
$element
->
$key
;
}
elseif
(
is_array
(
$element
))
{
$result
[]
=
$element
[
$key
];
}
}
}
return
$result
;
}
/**
* Builds a map (key-value pairs) from a multidimensional array or an array of objects.
* The `$from` and `$to` parameters specify the key names or property names to set up the map.
*
* For example,
*
* ~~~
* $array = array(
* array('id' => '123', 'data' => 'abc'),
* array('id' => '345', 'data' => 'def'),
* );
* $result = ArrayHelper::map($array, 'id', 'data');
* // the result is:
* // array(
* // '123' => 'abc',
* // '345' => 'def',
* // )
* ~~~
*
* @param $array
* @param $from
* @param $to
* @return array
*/
public
static
function
map
(
$array
,
$from
,
$to
)
{
$result
=
array
();
foreach
(
$array
as
$element
)
{
if
(
is_object
(
$element
))
{
$result
[
$element
->
$from
]
=
$element
->
$to
;
}
elseif
(
is_array
(
$element
))
{
$result
[
$element
[
$from
]]
=
$element
[
$to
];
}
}
return
$result
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment