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
7a5a29c7
Commit
7a5a29c7
authored
Dec 02, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ArrayHelper::keyExists().
parent
a351b13e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
BaseArrayHelper.php
framework/yii/helpers/BaseArrayHelper.php
+23
-0
ArrayHelperTest.php
tests/unit/framework/helpers/ArrayHelperTest.php
+17
-0
No files found.
framework/yii/helpers/BaseArrayHelper.php
View file @
7a5a29c7
...
@@ -327,6 +327,29 @@ class BaseArrayHelper
...
@@ -327,6 +327,29 @@ class BaseArrayHelper
}
}
/**
/**
* Checks if the given array contains the specified key.
* This method enhances the `array_key_exists()` function by supporting case-insensitive
* key comparison.
* @param string $key the key to check
* @param array $array the array with keys to check
* @param boolean $caseSensitive whether the key comparison should be case-sensitive
* @return boolean whether the array contains the specified key
*/
public
static
function
keyExists
(
$key
,
$array
,
$caseSensitive
=
true
)
{
if
(
$caseSensitive
)
{
return
array_key_exists
(
$key
,
$array
);
}
else
{
foreach
(
array_keys
(
$array
)
as
$k
)
{
if
(
strcasecmp
(
$key
,
$k
)
===
0
)
{
return
true
;
}
}
return
false
;
}
}
/**
* Sorts an array of objects or arrays (with the same structure) by one or several keys.
* Sorts an array of objects or arrays (with the same structure) by one or several keys.
* @param array $array the array to be sorted. The array will be modified after calling this method.
* @param array $array the array to be sorted. The array will be modified after calling this method.
* @param string|\Closure|array $key the key(s) to be sorted by. This refers to a key name of the sub-array
* @param string|\Closure|array $key the key(s) to be sorted by. This refers to a key name of the sub-array
...
...
tests/unit/framework/helpers/ArrayHelperTest.php
View file @
7a5a29c7
...
@@ -303,4 +303,21 @@ class ArrayHelperTest extends TestCase
...
@@ -303,4 +303,21 @@ class ArrayHelperTest extends TestCase
],
],
],
$result
);
],
$result
);
}
}
public
function
testKeyExists
()
{
$array
=
[
'a'
=>
1
,
'B'
=>
2
,
];
$this
->
assertTrue
(
ArrayHelper
::
keyExists
(
'a'
,
$array
));
$this
->
assertFalse
(
ArrayHelper
::
keyExists
(
'b'
,
$array
));
$this
->
assertTrue
(
ArrayHelper
::
keyExists
(
'B'
,
$array
));
$this
->
assertFalse
(
ArrayHelper
::
keyExists
(
'c'
,
$array
));
$this
->
assertTrue
(
ArrayHelper
::
keyExists
(
'a'
,
$array
,
false
));
$this
->
assertTrue
(
ArrayHelper
::
keyExists
(
'b'
,
$array
,
false
));
$this
->
assertTrue
(
ArrayHelper
::
keyExists
(
'B'
,
$array
,
false
));
$this
->
assertFalse
(
ArrayHelper
::
keyExists
(
'c'
,
$array
,
false
));
}
}
}
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