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
e2a57e4b
Commit
e2a57e4b
authored
May 09, 2013
by
Antonio Ramirez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhance ArrayHelper with popValue method
parent
ad26b7d3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
2 deletions
+41
-2
ArrayHelper.php
framework/helpers/base/ArrayHelper.php
+31
-2
ArrayHelperTest.php
tests/unit/framework/helpers/ArrayHelperTest.php
+10
-0
No files found.
framework/helpers/base/ArrayHelper.php
View file @
e2a57e4b
...
...
@@ -87,6 +87,35 @@ class ArrayHelper
}
/**
* Removes an item from an array and returns the value. If the key does not exist in the array, the default value
* will be returned instead.
*
* Usage examples,
*
* ~~~
* // $array = array('type'=>'A', 'options'=>array(1,2));
* // working with array
* $type = \yii\helpers\ArrayHelper::getValue($array, 'type');
* // $array content
* // $array = array('options'=>array(1,2));
* ~~~
*
* @param array $array the array to extract value from
* @param string $key key name of the array element
* @param mixed $default the default value to be returned if the specified key does not exist
* @return mixed|null the value of the element if found, default value otherwise
*/
public
static
function
popValue
(
&
$array
,
$key
,
$default
=
null
)
{
if
(
is_array
(
$array
))
{
$value
=
static
::
getValue
(
$array
,
$key
,
$default
);
unset
(
$array
[
$key
]);
return
$value
;
}
return
$default
;
}
/**
* Indexes an array according to a specified key.
* The input array should be multidimensional or an array of objects.
*
...
...
@@ -284,12 +313,12 @@ class ArrayHelper
$args
[]
=
$column
;
}
}
else
{
$args
[]
=
static
::
getColumn
(
$array
,
$key
);
$args
[]
=
static
::
getColumn
(
$array
,
$key
);
}
$args
[]
=
$ascending
[
$i
]
?
SORT_ASC
:
SORT_DESC
;
$args
[]
=
$flag
;
}
$args
[]
=
&
$array
;
$args
[]
=
&
$array
;
call_user_func_array
(
'array_multisort'
,
$args
);
}
...
...
tests/unit/framework/helpers/ArrayHelperTest.php
View file @
e2a57e4b
...
...
@@ -12,6 +12,16 @@ class ArrayHelperTest extends \yii\test\TestCase
}
public
function
testPopvalue
()
{
$array
=
array
(
'name'
=>
'b'
,
'age'
=>
3
);
$name
=
ArrayHelper
::
popValue
(
$array
,
'name'
);
$this
->
assertEquals
(
$name
,
'b'
);
$this
->
assertEquals
(
$array
,
array
(
'age'
=>
3
));
}
public
function
testMultisort
()
{
// single key
...
...
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