Hash โ
class Cake\Utility\Hash
้ ๅใใใธใกใณใใฏใจใฆใๅผทๅใใคไพฟๅฉใชใใผใซใงใใใ้ฉๅใซไฝฟใใใใใใฐใ ใใในใใผใใงใใๆ้ฉๅใใใใณใผใใๆธใใใจใใงใใใใใซใชใใใฎใงใใ CakePHP ใงใฏใจใฆใไพฟๅฉใชใฆใผใใฃใชใใฃ็พคใ Hash ใฏใฉในใฎไธญใซ static ใง็จๆใใฆใใใใพใใซใใใใใใฎใซไฝฟใใพใใ
CakePHP ใฎ Hash ใฏใฉในใฏ Inflector ใฏใฉในใจๅๆงใงใใฉใฎใขใใซใ ใณใณใใญใผใฉใผใใใงใๅผใถใใจใใงใใพใใ ไพ: Hash::combine() ใ
Hash ใในๆงๆ โ
ไธ่จใฎใในๆงๆใฏ Hash ใๆใคใในใฆใฎใกใฝใใใงไฝฟใใใใใฎใงใใ ใใ ใใใในใฆใฎใในๆงๆใใใในใฆใฎใกใฝใใใงไฝฟ็จๅฏ่ฝใงใใใจใฏ้ใใพใใใ ใในใฎๅผใฏใใใคใใฎใใผใฏใณใงๆงๆใใใพใใใใผใฏใณใฏใ้
ๅใใผใฟใฎ็งปๅใซไฝฟใใๅผใใจใ ่ฆ็ด ใ็ตใ่พผใใใใใใฃใผใใฎ๏ผใคใฎใฐใซใผใใซๅคงใใๅใใใใพใใ ใใใใฃใผใฏ่ฆ็ด ใฎๅผใซๅฏพใใฆ้ฉ็จใใใใจใใงใใพใใ
ๅผใฎ็จฎ้ก โ
| ๅผ | ่ชฌๆ |
|---|---|
{n} | ๆฐๅคใญใผใๆๅณใใใใฉใใชๆๅญๅใญใผใงใ ๆฐๅคๅใฎใญใผใงใไธ่ดใใใ |
{s} | ๆๅญๅใญใผใๆๅณใใใๆฐๅคๆๅญๅใๅซใใ ใฉใใชๆๅญๅใงใไธ่ดใใใ |
{*} | ไปปๆใฎๅคใจไธ่ดใใใ |
Foo | ๅฎๅ จใซๅใๅคใ ใฃใๅ ดๅใฎใฟไธ่ดใใใ |
่ฆ็ด ใฎๅผใฏใใใใใใในใฆใฎใกใฝใใใงไฝฟใใใจใใงใใพใใ็นๅฎใฎใกใฝใใใงใฏใ ่ฆ็ด ใฎๅผใซๅ ใใ ๅฑๆงใง็ตใ่พผใใใจใใงใใพใใ่ฉฒๅฝใใใกใฝใใใฏใ extract(), combine(), format(), check(), map(), reduce(), apply(), sort(), insert(), remove() ใจ nest() ใงใใ
ๅฑๆงใฎ็ตใ่พผใฟ็จฎๅฅ โ
| ใใใใฃใผ | ่ชฌๆ |
|---|---|
[id] | ่จ่ฟฐใใใใญใผใจไธ่ดใใ่ฆ็ด ใซ็ตใ่พผใใ |
[id=2] | id ใ 2 ใจใชใฃใฆใใ่ฆ็ด ใซ็ตใ่พผใใ |
[id!=2] | id ใ 2 ใงใฏใชใ่ฆ็ด ใซ็ตใ่พผใใ |
[id>2] | id ใ 2 ใใๅคงใใ่ฆ็ด ใซ็ตใ่พผใใ |
[id>=2] | id ใ 2 ไปฅไธใฎ่ฆ็ด ใซ็ตใ่พผใใ |
[id<2] | id ใ 2 ใใๅฐใใ่ฆ็ด ใซ็ตใ่พผใใ |
[id<=2] | id ใ 2 ไปฅไธใฎ่ฆ็ด ใซ็ตใ่พผใใ |
[text=/.../] | ๆญฃ่ฆ่กจ็พ ... ใจๅ่ดใใๅคใๆใฃใฆใใ ่ฆ็ด ใซ็ตใ่พผใใ |
static Cake\Utility\Hash::get(array|ArrayAccess $data, $path, $default = null)
get() ใฏ extract() ใฎใทใณใใซ็ใงใ็ดๆฅ็ใซๆๅฎใใใในๅผใฎใฟใใตใใผใใใใพใใ {n} ใ {s} ใ {*} ใใพใใฏใใใใใฃใผใไฝฟใฃใใในใฏใตใใผใใใใพใใใ ้
ๅใใ๏ผใคใฎๅคใ ใใๅใๅบใใใๅ ดๅใซ get() ใไฝฟใฃใฆใใ ใใใ ใใใใใใใใในใ่ฆใคใใใชใๅ ดๅใใใใฉใซใๅคใ่ฟใใพใใ
static Cake\Utility\Hash::extract(array|ArrayAccess $data, $path)
Hash::extract() ใฏ Hash Path Syntax ใซใใใในใฆใฎๅผใจใใใใฃใผใ ใตใใผใใใพใใextract ใไฝฟใใใจใงใ้
ๅใใใใฏ ArrayAccess ใคใณใฟใผใใงใคในใ ๅฎ่ฃ
ใใใชใใธใงใฏใใใๅฅฝใใชใในใซๆฒฟใฃใใใผใฟใๆๆฉใๅใๅบใใใจใใงใใพใใ ใใฏใใใผใฟๆง้ ใใซใผใใใๅฟ
่ฆใฏใใใพใใใใใฎไปฃใใใซๆฌฒใใ่ฆ็ด ใ็ตใ่พผใใในๅผใ ไฝฟใใฎใงใใ :
// ๆฎ้ใฎไฝฟใๆน:
$users = [
['id' => 1, 'name' => 'mark'],
['id' => 2, 'name' => 'jane'],
['id' => 3, 'name' => 'sally'],
['id' => 4, 'name' => 'jose'],
];
$results = Hash::extract($users, '{n}.id');
// $results ใฏไปฅไธใฎใจใใ:
// [1,2,3,4];static Hash::insert(array $data, $path, $values = null)
$values ใ $path ใฎๅฎ็พฉใซๅพใฃใฆ้
ๅใฎไธญใซๆฟๅ
ฅใใพใใ :
$a = [
'pages' => ['name' => 'page']
];
$result = Hash::insert($a, 'files', ['name' => 'files']);
// $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[pages] => [
[name] => page
]
[files] => [
[name] => files
]
]{n} ใ {s} ใใใฆ {*} ใไฝฟใฃใใในใไฝฟใใใจใงใ ่คๆฐใฎใใคใณใใซใใผใฟใๆฟๅ
ฅใใใใจใใงใใพใใ :
$users = Hash::insert($users, '{n}.new', 'value');insert() ใงใฏๅฑๆงใฎใใใใฃใผใๅใใพใใ :
$data = [
0 => ['up' => true, 'Item' => ['id' => 1, 'title' => 'first']],
1 => ['Item' => ['id' => 2, 'title' => 'second']],
2 => ['Item' => ['id' => 3, 'title' => 'third']],
3 => ['up' => true, 'Item' => ['id' => 4, 'title' => 'fourth']],
4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
];
$result = Hash::insert($data, '{n}[up].Item[id=4].new', 9);
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
['up' => true, 'Item' => ['id' => 1, 'title' => 'first']],
['Item' => ['id' => 2, 'title' => 'second']],
['Item' => ['id' => 3, 'title' => 'third']],
['up' => true, 'Item' => ['id' => 4, 'title' => 'fourth', 'new' => 9]],
['Item' => ['id' => 5, 'title' => 'fifth']],
]
*/static Cake\Utility\Hash::remove(array $data, $path)
$path ใซๅ่ดใใใในใฆใฎ่ฆ็ด ใ้
ๅใใๅ้คใใพใใ :
$a = [
'pages' => ['name' => 'page'],
'files' => ['name' => 'files']
];
$result = Hash::remove($a, 'files');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[pages] => [
[name] => page
]
]
*/{n} ใ {s} ใใใฆ {*} ใไฝฟใใใจใงใ่คๆฐใฎๅคใไธๅบฆใซๅ้คใใใใจใใงใใพใใ ใพใใremove() ใงใฏๅฑๆงใฎใใใใฃใผใไฝฟ็จใใใใจใใงใใพใใ :
$data = [
0 => ['clear' => true, 'Item' => ['id' => 1, 'title' => 'first']],
1 => ['Item' => ['id' => 2, 'title' => 'second']],
2 => ['Item' => ['id' => 3, 'title' => 'third']],
3 => ['clear' => true, 'Item' => ['id' => 4, 'title' => 'fourth']],
4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
];
$result = Hash::remove($data, '{n}[clear].Item[id=4]');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
['clear' => true, 'Item' => ['id' => 1, 'title' => 'first']],
['Item' => ['id' => 2, 'title' => 'second']],
['Item' => ['id' => 3, 'title' => 'third']],
['clear' => true],
['Item' => ['id' => 5, 'title' => 'fifth']],
]
*/static Cake\Utility\Hash::combine(array $data, $keyPath, $valuePath = null, $groupPath = null)
$keyPath ใฎใในใใญใผใ$valuePath ๏ผ็็ฅๅฏ๏ผ ใฎใในใๅคใจใใฆไฝฟใฃใฆ้ฃๆณ้
ๅใไฝใใพใใ $valuePath ใ็็ฅใใใๅ ดๅใใ$valuePath ใซๅ่ดใใใใฎใ็กใใฃใๅ ดๅใฏใๅคใฏ null ใงๅๆๅใใใพใใ $groupPath ใๆๅฎใใใๅ ดๅใฏใใใฎใในใซใใใใฃใฆ็ๆใใใใฎใใฐใซใผใใณใฐใใพใใ :
$a = [
[
'User' => [
'id' => 2,
'group_id' => 1,
'Data' => [
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
]
]
],
[
'User' => [
'id' => 14,
'group_id' => 2,
'Data' => [
'user' => 'phpnut',
'name' => 'Larry E. Masters'
]
]
],
];
$result = Hash::combine($a, '{n}.User.id');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[2] =>
[14] =>
]
*/
$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.user');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[2] => 'mariano.iglesias'
[14] => 'phpnut'
]
*/
$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[2] => [
[user] => mariano.iglesias
[name] => Mariano Iglesias
]
[14] => [
[user] => phpnut
[name] => Larry E. Masters
]
]
*/
$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[2] => Mariano Iglesias
[14] => Larry E. Masters
]
*/
$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[1] => [
[2] => [
[user] => mariano.iglesias
[name] => Mariano Iglesias
]
]
[2] => [
[14] => [
[user] => phpnut
[name] => Larry E. Masters
]
]
]
*/
$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[1] => [
[2] => Mariano Iglesias
]
[2] => [
[14] => Larry E. Masters
]
]
*/
// As of 3.9.0 $keyPath can be null
$result = Hash::combine($a, null, '{n}.User.Data.name');
/* $result now looks like:
[
[0] => Mariano Iglesias
[1] => Larry E. Masters
]
*/$keyPath ใใใณ $valuePath ใง้
ๅใๆๅฎใใใใจใใงใใพใใใใใซใใใ ๆๅใฎ่ฆ็ด ใงๆๅฎใใๅฝขๅผใซๅใใใฆใใใฎไปใฎใในใงๆๅฎใใๅคใใใฉใผใใใใใใพใใ :
$result = Hash::combine(
$a,
'{n}.User.id',
['%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'],
'{n}.User.group_id'
);
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[1] => [
[2] => mariano.iglesias: Mariano Iglesias
]
[2] => [
[14] => phpnut: Larry E. Masters
]
]
*/
$result = Hash::combine(
$a,
['%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'],
'{n}.User.id'
);
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[mariano.iglesias: Mariano Iglesias] => 2
[phpnut: Larry E. Masters] => 14
]
*/static Cake\Utility\Hash::format(array $data, array $paths, $format)
้ ๅใใๅใๅบใใใใฉใผใใใๆๅญๅใงใใฉใผใใใใใใๆๅญๅใฎ้ ๅใ่ฟใใพใใ :
$data = [
[
'Person' => [
'first_name' => 'Nate',
'last_name' => 'Abele',
'city' => 'Boston',
'state' => 'MA',
'something' => '42'
]
],
[
'Person' => [
'first_name' => 'Larry',
'last_name' => 'Masters',
'city' => 'Boondock',
'state' => 'TN',
'something' => '{0}'
]
],
[
'Person' => [
'first_name' => 'Garrett',
'last_name' => 'Woodworth',
'city' => 'Venice Beach',
'state' => 'CA',
'something' => '{1}'
]
]
];
$res = Hash::format($data, ['{n}.Person.first_name', '{n}.Person.something'], '%2$d, %1$s');
/*
[
[0] => 42, Nate
[1] => 0, Larry
[2] => 0, Garrett
]
*/
$res = Hash::format($data, ['{n}.Person.first_name', '{n}.Person.something'], '%1$s, %2$d');
/*
[
[0] => Nate, 42
[1] => Larry, 0
[2] => Garrett, 0
]
*/static Cake\Utility\Hash::contains(array $data, array $needle)
ไธๆนใฎใใใทใฅใ้ ๅใฎไธญใซใใใไธๆนใฎใญใผใจๅคใๅณๅฏใซ่ฆใฆใในใฆๅญๅจใใฆใใใใๅคๅฎใใพใใ :
$a = [
0 => ['name' => 'main'],
1 => ['name' => 'about']
];
$b = [
0 => ['name' => 'main'],
1 => ['name' => 'about'],
2 => ['name' => 'contact'],
'a' => 'b'
];
$result = Hash::contains($a, $a);
// true
$result = Hash::contains($a, $b);
// false
$result = Hash::contains($b, $a);
// truestatic Cake\Utility\Hash::check(array $data, string $path = null)
้ ๅใฎไธญใซ็นๅฎใฎใในใใปใใใใใฆใใใใใใงใใฏใใพใใ :
$set = [
'My Index 1' => ['First' => 'The first item']
];
$result = Hash::check($set, 'My Index 1.First');
// $result == true
$result = Hash::check($set, 'My Index 1');
// $result == true
$set = [
'My Index 1' => [
'First' => [
'Second' => [
'Third' => [
'Fourth' => 'Heavy. Nesting.'
]
]
]
]
];
$result = Hash::check($set, 'My Index 1.First.Second');
// $result == true
$result = Hash::check($set, 'My Index 1.First.Second.Third');
// $result == true
$result = Hash::check($set, 'My Index 1.First.Second.Third.Fourth');
// $result == true
$result = Hash::check($set, 'My Index 1.First.Seconds.Third.Fourth');
// $result == falsestatic Cake\Utility\Hash::filter(array $data, $callback = ['Hash', 'filter'])
้
ๅใใ็ฉบใฎ่ฆ็ด ๏ผใใ ใ '0' ไปฅๅค๏ผใๅใ้คใใพใใ ใพใใใซในใฟใ ๅผๆฐ $callback ใๆๅฎใใใใจใง้
ๅใฎ่ฆ็ด ใๆฝๅบใใใใจใใงใใพใใ ใณใผใซใใใฏ้ขๆฐใ false ใ่ฟใใๅ ดๅใใใฎ่ฆ็ด ใฏ้
ๅใใๅใ้คใใใพใใ :
$data = [
'0',
false,
true,
0,
['one thing', 'I can tell you', 'is you got to be', false]
];
$res = Hash::filter($data);
/* $res ใฏไปฅไธใฎใใใซใชใใพใ:
[
[0] => 0
[2] => true
[3] => 0
[4] => [
[0] => one thing
[1] => I can tell you
[2] => is you got to be
]
]
*/static Cake\Utility\Hash::flatten(array $data, string $separator = '.')
ๅคๆฌกๅ ้ ๅใ๏ผๆฌกๅ ้ ๅใธใจๅนณๅฆๅใใพใใ :
$arr = [
[
'Post' => ['id' => '1', 'title' => 'First Post'],
'Author' => ['id' => '1', 'user' => 'Kyle'],
],
[
'Post' => ['id' => '2', 'title' => 'Second Post'],
'Author' => ['id' => '3', 'user' => 'Crystal'],
],
];
$res = Hash::flatten($arr);
/* $res ใฏไปฅไธใฎใใใซใชใใพใ:
[
[0.Post.id] => 1
[0.Post.title] => First Post
[0.Author.id] => 1
[0.Author.user] => Kyle
[1.Post.id] => 2
[1.Post.title] => Second Post
[1.Author.id] => 3
[1.Author.user] => Crystal
]
*/static Cake\Utility\Hash::expand(array $data, string $separator = '.')
Hash::flatten() ใซใใฃใฆๅใใฃใฆๅนณๅฆๅใใใ้
ๅใๅๆง็ฏใใพใใ :
$data = [
'0.Post.id' => 1,
'0.Post.title' => First Post,
'0.Author.id' => 1,
'0.Author.user' => Kyle,
'1.Post.id' => 2,
'1.Post.title' => Second Post,
'1.Author.id' => 3,
'1.Author.user' => Crystal,
];
$res = Hash::expand($data);
/* $res ใฏไปฅไธใฎใใใซใชใใพใ:
[
[
'Post' => ['id' => '1', 'title' => 'First Post'],
'Author' => ['id' => '1', 'user' => 'Kyle'],
],
[
'Post' => ['id' => '2', 'title' => 'Second Post'],
'Author' => ['id' => '3', 'user' => 'Crystal'],
],
];
*/static Cake\Utility\Hash::merge(array $data, array $merge[, array $n])
ใใฎ้ขๆฐใฏ PHP ใฎ array_merge ใจ array_merge_recursive ใฎ ไธกๆนใฎๆฉ่ฝใๆใฃใฆใใใจ่ใใใใจใใงใใพใใใใฎ๏ผใคใฎ้ขๆฐใจใฎ้ใใฏใไธๆนใฎ้
ๅใญใผใ ใใไธๆนใซๅซใพใใฆใใๅ ดๅใซใฏ (array_merge ใจ้ใฃใฆ) ๅๅธฐ็ใซๅใใพใใใ ๅซใพใใฆใใชใใฃใๅ ดๅใซใฏ (array_merge_recursive ใจ้ใฃใฆ) ๅๅธฐ็ใซใฏๅใใพใใใ
NOTE
ใใฎ้ขๆฐใฎๅผๆฐใฎๅๆฐใซๅถ้ใฏใใใพใใใใพใใ้ ๅไปฅๅคใๅผๆฐใซๆๅฎใใใๅ ดๅใฏ ้ ๅใธใจใญใฃในใใใใพใใ
$array = [
[
'id' => '48c2570e-dfa8-4c32-a35e-0d71cbdd56cb',
'name' => 'mysql raleigh-workshop-08 < 2008-09-05.sql ',
'description' => 'Importing an sql dump'
],
[
'id' => '48c257a8-cf7c-4af2-ac2f-114ecbdd56cb',
'name' => 'pbpaste | grep -i Unpaid | pbcopy',
'description' => 'Remove all lines that say "Unpaid".',
]
];
$arrayB = 4;
$arrayC = [0 => "test array", "cats" => "dogs", "people" => 1267];
$arrayD = ["cats" => "felines", "dog" => "angry"];
$res = Hash::merge($array, $arrayB, $arrayC, $arrayD);
/* $res ใฏไปฅไธใฎใใใซใชใใพใ:
[
[0] => [
[id] => 48c2570e-dfa8-4c32-a35e-0d71cbdd56cb
[name] => mysql raleigh-workshop-08 < 2008-09-05.sql
[description] => Importing an sql dump
]
[1] => [
[id] => 48c257a8-cf7c-4af2-ac2f-114ecbdd56cb
[name] => pbpaste | grep -i Unpaid | pbcopy
[description] => Remove all lines that say "Unpaid".
]
[2] => 4
[3] => test array
[cats] => felines
[people] => 1267
[dog] => angry
]
*/static Cake\Utility\Hash::numeric(array $data)
้ ๅๅ ใฎใในใฆใฎๅคใๆฐๅคใงใใใใใใงใใฏใใพใใ :
$data = ['one'];
$res = Hash::numeric(array_keys($data));
// $res ใฏ true
$data = [1 => 'one'];
$res = Hash::numeric($data);
// $res ใฏ falsestatic Cake\Utility\Hash:๐*dimensions **(array $data)
้ ๅใฎๆฌกๅ ๆฐใๆฐใใพใใใใฎใกใฝใใใฏ้ ๅใฎ๏ผใค็ฎใฎ่ฆ็ด ใ ใใ่ฆใฆๆฌกๅ ใๅคๅฎใใพใใ :
$data = ['one', '2', 'three'];
$result = Hash::dimensions($data);
// $result == 1
$data = ['1' => '1.1', '2', '3'];
$result = Hash::dimensions($data);
// $result == 1
$data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => '3.1.1']];
$result = Hash::dimensions($data);
// $result == 2
$data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']];
$result = Hash::dimensions($data);
// $result == 1
$data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
$result = Hash::dimensions($data);
// $result == 2static Cake\Utility\Hash::maxDimensions(array $data)
~Hash::dimensions() ใซไผผใฆใใพใใใใใฎใกใฝใใใฏ้
ๅๅ
ใซใใ ใใฃใจใๅคงใใชๆฌกๅ
ๆฐใ่ฟใใพใใ :
$data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']];
$result = Hash::maxDimensions($data);
// $result == 2
$data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
$result = Hash::maxDimensions($data);
// $result == 3static Cake\Utility\Hash::map(array $data, $path, $function)
$path ใงๆฝๅบใใๅ่ฆ็ด ใซ $function ใๅฒใๅฝใฆ๏ผmap๏ผใใใจใงๆฐใใช้
ๅใไฝใใพใใ ใใฎใกใฝใใใงใฏๅผใจใใใใฃใผใฎไธกๆนใไฝฟใใใจใใงใใพใใ :
// $data ใฎใในใฆใฎ่ฆ็ด ใซๅฏพใใฆ noop ้ขๆฐ $this->noop() ใๅผใณใพใใ
$result = Hash::map($data, "{n}", [$this, 'noop']);
public function noop(array $array)
{
// ้
ๅใซ่ฉฐใใฆใ็ตๆใ่ฟใใฆใใ ใใใ
return $array;
}static Cake\Utility\Hash::reduce(array $data, $path, $function)
$path ใงๆฝๅบใใๆฝๅบ็ตๆใ $function ใง็ธฎๅฐ๏ผreduce๏ผใใใใจใงใๅไธใฎๅคใไฝใใพใใ ใใฎใกใฝใใใงใฏๅผใจใใใใฃใผใฎไธกๆนใไฝฟใใใจใใงใใพใใ
static Cake\Utility\Hash::apply(array $data, $path, $function)
$function ใไฝฟ็จใใฆใๆฝๅบใใใๅคใฎใปใใใซใณใผใซใใใฏใ้ฉ็จใใพใใ ใใฎ้ขๆฐใฏ็ฌฌไธๅผๆฐใจใใฆๆฝๅบใใใๅคใๅๅพใใพใใ :
$data = [
['date' => '01-01-2016', 'booked' => true],
['date' => '01-01-2016', 'booked' => false],
['date' => '02-01-2016', 'booked' => true]
];
$result = Hash::apply($data, '{n}[booked=true].date', 'array_count_values');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
'01-01-2016' => 1,
'02-01-2016' => 1,
]
*/static Cake\Utility\Hash::sort(array $data, $path, $dir, $type = 'regular')
Hash Path Syntax ใซใใฃใฆใใฉใฎๆฌกๅ ใฎใฉใฎๅคใซใใฃใฆใงใใฝใผใใใใใจใใงใใพใใ ใใฎใกใฝใใใงใฏๅผใฎใฟใใตใใผใใใใพใใ :
$a = [
0 => ['Person' => ['name' => 'Jeff']],
1 => ['Shirt' => ['color' => 'black']]
];
$result = Hash::sort($a, '{n}.Person.name', 'asc');
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[0] => [
[Shirt] => [
[color] => black
]
]
[1] => [
[Person] => [
[name] => Jeff
]
]
]
*/$dir ใซใฏ asc ใใใใฏ desc ใๆๅฎใใใใจใใงใใพใใ $type ใซใฏๆฌกใฎใใใใใๆๅฎใใใใจใใงใใพใใ
regular: ้ๅธธใฎใฝใผใใnumeric: ๆฐๅคใจใฟใชใใฆใฝใผใใstring: ๆๅญๅใจใใฆใฝใผใใnatural: ใใฅใผใใณใปใใฌใณใใชใผใปใฝใผใใไพใใฐใfoo10ใfoo2ใฎไธใซ้ ็ฝฎใใใใ
static Cake\Utility\Hash::diff(array $data, array $compare)
๏ผใคใฎ้ ๅใฎๅทฎๅใ่จ็ฎใใพใ:
$a = [
0 => ['name' => 'main'],
1 => ['name' => 'about']
];
$b = [
0 => ['name' => 'main'],
1 => ['name' => 'about'],
2 => ['name' => 'contact']
];
$result = Hash::diff($a, $b);
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[2] => [
[name] => contact
]
]
*/static Cake\Utility\Hash::mergeDiff(array $data, array $compare)
ใใฎ้ขๆฐใฏ๏ผใคใฎ้ ๅใใใผใธใใๅทฎๅใฏใใใฎ็ตๆใฎ้ ๅใฎไธ้จใซ push ใใพใใ
ไพ๏ผ :
$array1 = ['ModelOne' => ['id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2']];
$array2 = ['ModelOne' => ['id' => 1003, 'field_one' => 'a3.m1.f1', 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3']];
$res = Hash::mergeDiff($array1, $array2);
/* $res ใฏไปฅไธใฎใใใซใชใใพใ:
[
[ModelOne] => [
[id] => 1001
[field_one] => a1.m1.f1
[field_two] => a1.m1.f2
[field_three] => a3.m1.f3
]
]
*/ไพ๏ผ :
$array1 = ["a" => "b", 1 => 20938, "c" => "string"];
$array2 = ["b" => "b", 3 => 238, "c" => "string", ["extra_field"]];
$res = Hash::mergeDiff($array1, $array2);
/* $res ใฏไปฅไธใฎใใใซใชใใพใ:
[
[a] => b
[1] => 20938
[c] => string
[b] => b
[3] => 238
[4] => [
[0] => extra_field
]
]
*/static Cake\Utility\Hash::normalize(array $data, $assoc = true)
้
ๅใๆญฃ่ฆๅใใพใใ $assoc ใ true ใชใใ้ฃๆณ้
ๅใธใจๆญฃ่ฆๅใใใ้
ๅใ ่ฟใใพใใๅคใๆใคๆฐๅคใญใผใฏ null ใๆใคๆๅญๅใญใผใธใจๅคๆใใใพใใ ้
ๅใๆญฃ่ฆๅใใใจใ Hash::merge() ใงๆฑใใใใใชใใพใใ :
$a = ['Tree', 'CounterCache',
'Upload' => [
'folder' => 'products',
'fields' => ['image_1_id', 'image_2_id']
]
];
$result = Hash::normalize($a);
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[Tree] => null
[CounterCache] => null
[Upload] => [
[folder] => products
[fields] => [
[0] => image_1_id
[1] => image_2_id
]
]
]
*/
$b = [
'Cacheable' => ['enabled' => false],
'Limit',
'Bindable',
'Validator',
'Transactional'
];
$result = Hash::normalize($b);
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
[Cacheable] => [
[enabled] => false
]
[Limit] => null
[Bindable] => null
[Validator] => null
[Transactional] => null
]
*/static Cake\Utility\Hash::nest(array $data, array $options = [])
ๅนณๅฆใช้ ๅใใใๅคๆฌกๅ ้ ๅใใใใฏในใฌใใ็ถ๏ผthreaded๏ผใฎๆง้ ๅใใผใฟใ็ๆใใพใใ
ใชใใทใงใณ:
children: ๅญใฎ้ ๅใฎใใใซไฝฟใใใๆปใๅคใฎใญใผๅใใใใฉใซใใฏ 'children'ใidPath: ๅ่ฆ็ด ใ่ญๅฅใใใใใฎใญใผใๆใใในใHash::extract()ใจๅๆงใซๆๅฎใใใใใใฉใซใใฏ{n}.$alias.idparentPath: ๅ่ฆ็ด ใฎ่ฆชใ่ญๅฅใใใใใฎใญใผใๆใใในใHash::extract()ใจๅๆงใซๆๅฎใใใใใใฉใซใใฏ{n}.$alias.parent_idroot: ๆไธไฝใจใชใ่ฆ็ด ใฎ id ใ
ๆฌกใฎ้ ๅใใผใฟใไฝฟ็จใใไพ:
$data = [
['ThreadPost' => ['id' => 1, 'parent_id' => null]],
['ThreadPost' => ['id' => 2, 'parent_id' => 1]],
['ThreadPost' => ['id' => 3, 'parent_id' => 1]],
['ThreadPost' => ['id' => 4, 'parent_id' => 1]],
['ThreadPost' => ['id' => 5, 'parent_id' => 1]],
['ThreadPost' => ['id' => 6, 'parent_id' => null]],
['ThreadPost' => ['id' => 7, 'parent_id' => 6]],
['ThreadPost' => ['id' => 8, 'parent_id' => 6]],
['ThreadPost' => ['id' => 9, 'parent_id' => 6]],
['ThreadPost' => ['id' => 10, 'parent_id' => 6]]
];
$result = Hash::nest($data, ['root' => 6]);
/* $result ใฏไปฅไธใฎใใใซใชใใพใ:
[
(int) 0 => [
'ThreadPost' => [
'id' => (int) 6,
'parent_id' => null
],
'children' => [
(int) 0 => [
'ThreadPost' => [
'id' => (int) 7,
'parent_id' => (int) 6
],
'children' => []
],
(int) 1 => [
'ThreadPost' => [
'id' => (int) 8,
'parent_id' => (int) 6
],
'children' => []
],
(int) 2 => [
'ThreadPost' => [
'id' => (int) 9,
'parent_id' => (int) 6
],
'children' => []
],
(int) 3 => [
'ThreadPost' => [
'id' => (int) 10,
'parent_id' => (int) 6
],
'children' => []
]
]
]
]
*/