ΠΠ°ΠΊ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π΅ΡΡΡ Π»ΠΈ ΠΈΠ½Π΄Π΅ΠΊΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ php
ΠΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π° ΠΌΠ°ΡΡΠΈΠ², Π½Π° Π½Π°Π»ΠΈΡΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΈ Π½Π° ΠΏΡΡΡΠΎΡΡ Π² PHP
Π ΡΡΠΎΠΉ ΡΡΠ°ΡΡΠ΅ Π±ΡΠ΄Π΅ΠΌ Π΄Π΅Π»Π°ΡΡ ΡΠ°Π·Π»ΠΈΡΠ½ΡΠ΅ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ ΠΌΠ°ΡΡΠΈΠ²Π°. Π ΡΠΎΠΌ ΡΠΈΡΠ»Π΅ ΠΏΡΠΎΠ²Π΅ΡΠΈΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ Π»ΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ, Π° ΡΠ°ΠΊ ΠΆΠ΅ ΠΏΡΠΎΠ²Π΅ΡΠΈΠΌ Π΅ΡΡΡ Π»ΠΈ Ρ ΠΊΠ»ΡΡΠ΅ΠΉ ΠΌΠ°ΡΡΠΈΠ²Π° ΠΏΡΡΡΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ. ΠΡΠ΄Π΅ΠΌ Π΄Π²ΠΈΠ³Π°ΡΡΡΡ ΠΎΡ ΠΏΡΠΎΡΡΠΎΠ³ΠΎ ΠΊ Π±ΠΎΠ»Π΅Π΅ ΡΠ»ΠΎΠΆΠ½ΠΎΠΌΡ. Π ΠΏΠ΅ΡΠ²ΠΎΠ΅, ΡΡΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ, ΡΡΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π° ΠΌΠ°ΡΡΠΈΠ². ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ Π½Π°ΠΌ ΠΏΠΎΠΌΠΎΠ΅Ρ Π²ΡΡΡΠΎΠ΅Π½Π½Π°Ρ Π² ΡΠ·ΡΠΊ PHP ΡΡΠ½ΠΊΡΠΈΡ is_array. Π Π°Π·Π±Π΅ΡΠ΅ΠΌ Π½Π΅Π±ΠΎΠ»ΡΡΠΎΠΉ ΠΏΡΠΈΠΌΠ΅Ρ.
$arr = [‘id’, ‘name’, ’email’]; // ΠΠ°ΡΡΠΈΠ² ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² if(is_array($arr))< echo 'ΠΡΠΎ ΠΌΠ°ΡΡΠΈΠ²'; >else
Π€ΡΠ½ΠΊΡΠΈΡ Π²Π΅ΡΠ½Π΅Ρ true, Π΅ΡΠ»ΠΈ ΡΡΠΎ ΠΌΠ°ΡΡΠΈΠ² ΠΈ false β Π΅ΡΠ»ΠΈ Π½Π΅ ΠΌΠ°ΡΡΠΈΠ². ΠΡΠΎ ΠΏΡΠΎΡΡΠΎΠΉ ΠΏΡΠΈΠΌΠ΅Ρ ΠΈ ΡΠ»ΠΎΠΆΠ½ΠΎΡΡΠ΅ΠΉ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ. ΠΠ΅ΡΠ΅ΠΉΠ΄Π΅ΠΌ ΠΊ ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΌΡ ΠΏΡΠΈΠΌΠ΅ΡΡ.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΠΌΠ°ΡΡΠΈΠ²Π° Π½Π° ΠΏΡΡΡΠΎΡΡ ΠΈ ΠΏΡΡΡΡΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ Π² PHP
Π€ΡΠ½ΠΊΡΠΈΡ empty ΡΡΠ°Π±ΠΎΡΠ°Π΅Ρ ΡΠΎΠ»ΡΠΊΠΎ Π² ΡΠΎΠΌ ΡΠ»ΡΡΠ°Π΅, ΠΊΠΎΠ³Π΄Π° Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ Π½Π΅Ρ Π²ΠΎΠΎΠ±ΡΠ΅ Π½ΠΈΠΊΠ°ΠΊΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ². ΠΠΎΡΡΠΎΠΌΡ Π΅Π΅ Π»ΡΡΡΠ΅ Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΏΡΠΈ ΠΏΡΠΎΠ²Π΅ΡΠΊΠ΅ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠ². Π ΡΡΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π½Π°ΠΌ ΠΏΠΎΠΌΠΎΠΆΠ΅Ρ Π΅ΡΠ΅ ΠΎΠ΄Π½Π° ΡΡΠ½ΠΊΡΠΈΡ β array_diff. Π‘ Π΅Π΅ ΠΏΠΎΠΌΠΎΡΡΡ ΠΌΡ ΡΡΠ°Π²Π½ΠΈΠΌ ΠΈΡΡ ΠΎΠ΄Π½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ² Ρ Π΄ΡΡΠ³ΠΈΠΌ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΠ°ΡΡ ΠΎΠΆΠ΄Π΅Π½ΠΈΡ Π²ΡΠ²Π΅Π΄Π΅ΠΌ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ ΠΌΠ°ΡΡΠΈΠ²Π°, ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π΅ ΡΠΎΠ²ΠΏΠ°Π΄Π°ΡΡ. ΠΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π½Π΅ΠΌΠ½ΠΎΠ³ΠΎ ΡΡΠΌΠ±ΡΡΠ½ΠΎ ΠΈ Π½Π΅ ΠΏΠΎΠ½ΡΡΠ½ΠΎ, Π½ΠΎ Π΄Π°Π²Π°ΠΉΡΠ΅ ΡΠ°Π·Π±Π΅ΡΠ΅ΠΌ Π½Π° ΠΏΡΠΈΠΌΠ΅ΡΠ΅.
array(8) < [0]=>string(4) «name» [1]=> string(0) «» [2]=> string(3) «num» [3]=> [4]=> string(0) «» [5]=> [6]=> string(4) «Alex» [7]=> string(0) «» > array(3) < [0]=>string(4) «name» [2]=> string(3) «num» [6]=> string(4) «Alex» >
Π€ΡΠ½ΠΊΡΠΈΡ ΠΏΡΠΎΠΏΡΡΡΠΈΡ Π²ΡΠ΅ ΠΏΡΡΡΡΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΌΠ°ΡΡΠΈΠ²Π°, Π² ΡΠΎΠΌ ΡΠΈΡΠ»Π΅ NULL ΠΈ false ΠΈ Π²ΡΠ²Π΅Π΄Π΅Ρ ΡΠΎΠ»ΡΠΊΠΎ ΡΠ΅, Π² ΠΊΠΎΡΠΎΡΡΡ ΡΡΠΎ-ΡΠΎ Π΅ΡΡΡ. ΠΡ ΡΠ°ΠΊ ΠΆΠ΅ ΠΌΠΎΠΆΠ΅ΠΌ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ ΠΊΠ°ΠΊΠΎΠΉ ΠΈΠΌΠ΅Π½Π½ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΌΠ°ΡΡΠΈΠ²Π° Π±ΡΠ» ΠΏΡΡΡΠΎΠΉ Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΡΠΈΠΊΠ»Π° for.
array(4) < ["age"]=>int(34) [«name»]=> string(4) «Ivan» [«city»]=> string(0) «» [«number»]=> string(0) «» > array(2) < ["age"]=>int(34) [«name»]=> string(4) «Ivan» >
ΠΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π°Π»ΠΈΡΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π² ΠΌΠ°ΡΡΠΈΠ²Π΅
ΠΠ»Ρ ΡΡΠΈΡ ΡΠ΅Π»Π΅ΠΉ ΠΏΠΎΠ΄ΠΎΠΉΠ΄Π΅Ρ ΡΡΠ½ΠΊΡΠΈΡ in_array. ΠΠ½Π° ΠΏΡΠΎΠ²Π΅ΡΡΠ΅Ρ Π½Π° ΠΏΡΠΈΡΡΡΡΡΠ²ΠΈΠ΅ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ.
ΠΠ΅ΡΠ²ΡΠΌ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠΌ ΡΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΡΡΠΎ Π±ΡΠ΄Π΅ΠΌ ΠΈΡΠΊΠ°ΡΡ, Π²ΡΠΎΡΡΠΌ β Π³Π΄Π΅. ΠΠ° ΡΡΠΎΠΌ ΠΏΠΎ ΡΠ°Π±ΠΎΡΠ΅ Ρ ΠΌΠ°ΡΡΠΈΠ²Π°ΠΌΠΈ ΠΎΠΊΠΎΠ½ΡΠ΅Π½. ΠΠ°Π΄Π΅ΡΡΡ, ΡΡΠΎ Π²Π°ΠΌ Π² Π½Π΅ΠΌ Π²ΡΠ΅ Π±ΡΠ»ΠΎ ΠΏΠΎΠ½ΡΡΠ½ΠΎ. ΠΠΎ Π΅ΡΠ»ΠΈ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΊΠ°ΠΊΠΈΠ΅-ΡΠΎ Π²ΠΎΠΏΡΠΎΡΡ, Π·Π°Π΄Π°Π²Π°ΠΉΡΠ΅ ΠΈΡ Π² ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΡΡ .
array_key_exists
(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)
array_key_exists β ΠΡΠΎΠ²Π΅ΡΡΠ΅Ρ, ΠΏΡΠΈΡΡΡΡΡΠ²ΡΠ΅Ρ Π»ΠΈ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΠΊΠ»ΡΡ ΠΈΠ»ΠΈ ΠΈΠ½Π΄Π΅ΠΊΡ
ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅
Π‘ΠΏΠΈΡΠΎΠΊ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ²
ΠΠ°ΡΡΠΈΠ² Ρ ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌΡΠΌΠΈ ΠΊΠ»ΡΡΠ°ΠΌΠΈ.
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ true Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅ΡΠ½ΠΎΠ³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΈΠ»ΠΈ false Π² ΡΠ»ΡΡΠ°Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΠΎΠ²Π΅Π½ΠΈΡ ΠΎΡΠΈΠ±ΠΊΠΈ.
array_key_exists() ΠΈΡΠ΅Ρ ΠΊΠ»ΡΡΠΈ ΡΠΎΠ»ΡΠΊΠΎ Π½Π° ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΡΠΎΠ²Π½Π΅ ΠΌΠ°ΡΡΠΈΠ²Π°. ΠΠ½ΡΡΡΠ΅Π½Π½ΠΈΠ΅ ΠΊΠ»ΡΡΠΈ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΠ½ΡΡ ΠΌΠ°ΡΡΠΈΠ²Π°Ρ Π½Π°ΠΉΠ΄Π΅Π½Ρ Π½Π΅ Π±ΡΠ΄ΡΡ.
ΠΡΠΈΠΌΠ΅ΡΡ
ΠΡΠΈΠΌΠ΅Ρ #1 ΠΡΠΈΠΌΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ array_key_exists()
ΠΡΠΈΠΌΠ΅Ρ #2 array_key_exists() ΠΈ isset()
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΡ
Π‘ΠΌΠΎΡΡΠΈΡΠ΅ ΡΠ°ΠΊΠΆΠ΅
User Contributed Notes 38 notes
If you want to take the performance advantage of isset() while keeping the NULL element correctly detected, use this:
Benchmark (100000 runs):
array_key_exists() : 205 ms
is_set() : 35ms
isset() || array_key_exists() : 48ms
Note:
The code for this check is very fast, so you shouldn’t warp the code into a single function like below, because the overhead of calling a function dominates the overall performance.
function array_check(. )
<
return (isset(..) || array_key_exists(. ))
>
You’ll notice several notes on this page stating that isset() is significantly faster than array_key_exists(). This may be true except for one small hitch. isset() will return false for arrays keys that have there value set to NULL, which is therefore not entirely accurate.
= array();
$foo [ ‘bar’ ] = NULL ;
Beware that if the array passed to array_key_exists is NULL, the return value will also be NULL.
This is undocumented behaviour, moreover the documentation (and return typehint) suggest that the array_key_exists function only returns boolean value. But that’s not the case.
The way array_key_exists handles null, float, boolean, and ‘integer-representing string’ keys is inconsistent in itself and, in the case of bool and float, with the way these are converted when used as array offset.
array (
» => 1,
0 => 2,
1 => 3,
4 => 4,
’08’ => 5,
8 => 6,
)
null is a key.
false is not a key.
true is not a key.
4.6 is not a key.
«08» is a key.
«8» is a key.
Well, and you get this warning three times (on the bools and the float, but not on the null):
Warning: array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integer in /var/www/php/test.php on line 6
The argument of array_key_exists() vs. isset() came up in the workplace today, so I conducted a little benchmark to see which is faster:
?>
On Windows, the output is similar to
array_key_exists(): 0.504 [82.895%] seconds
isset(): 0.104 [17.105%] seconds
On Mac or Linux, isset() is faster but only by a factor of approximately 1.5.
I’ve got a new take on the multi key function I would like to share.
Very simple case-insensitive array_key_exists:
bool (in_array(strtolower($needle), array_map(‘strtolower’, array_keys($haystack))))
array_key_exists doesn’t work with objects implementing ArrayAccess interface. It also ignores possible __get() method in such objects, despite the fact it accepts object as a second parameter. It works only with ‘real’ properties.
Here is an example with array_key_exists switching between content-types :
I took hours for me to debug, and I finally recognized that,
Or you will get no reply.
Rudi’s multidimensional array_key_exists function was not working for me, so i built one that is.
Enjoy.
Here is a little function for case sensitivity to elaborate on what was said by MarkL from ##php (Freenode) and mmanning at mdanderson dot org from this page:
Also, I’ve been running into issues with escaping for Regex, so I decided to give something like this a shot:
Regarding performance differences between isset() and array_key_exists(), the differences may be there, but the function are not always interchangable.
A little function which take an array as keys
Here’s a function to return a reference to the first array element that has a given key. The code works for multidimensional arrays:
I created this function that uses array key exist to compare a form and a table to see if something has changed.
This can be very helpfull if you need to update a table record from a form but you do not want to display all table fields.
/// it works like array_key_exists that can go deeper
$cidade = array(
‘redonda’ => array(
‘curta’ => ‘o seu filme’
),
‘quadrada’ => array(
‘longa’ => array(
‘azul’ => array(‘logo’,2,’mais’,2,’sΓ£o’,4),
‘amarela’ => array(‘entΓ£o’,3,’vezes’,2,’sΓ£o’,6),
‘verde’ => array(‘senΓ£o’,100,’dividido por’,2,’Γ©’,50)
),
‘extravagante’ => array(
‘vermelha’ => ‘chama atenΓ§Γ£o’,
‘vinho’ => ‘cor de uva’,
‘ocre’ => 1255
),
‘comprida’ => array(
‘amarela’ => ‘brasilia dos mamonas’,
‘branca’ => ‘bandeira da paz’,
‘preta e branca’ => ‘peΓ§as do xadrez’
)
),
‘oval’ => array(
‘conde’ => ‘lobo’
),
‘plana’ => array(
‘curta’ => array(
‘azul’ => array(‘e’,2,’mais’,2,’sΓ£o’,4),
‘amarela’ => array(‘sim’,3,’vezes’,2,’sΓ£o’,6),
‘verde’ => array(‘verdade’,100,’dividido por’,2,’Γ©’,50)
)
)
);
/// if the tree you search for exists, it will print out ‘true’
Further research on this has turned up that the performance problems are a known, confirmed bug in PHP 5.1.x, and have been fixed in PHP builds after September 2006. You can find the bug report here: http://bugs.php.net/bug.php?id=38812
However, just because it’s a fixed bug doesn’t really change the conclusion. If you’re writing a script and there’s any chance it could be used on a PHP 5.1.x server, you should still avoid this function and use isset() or some other kind of test if you want it to run efficiently.
I saw some examples above for array_keys_exist() or functions to see if multiple keys exist in a given array and return false if any of them don’t.
Here is a simpler way to do this:
array_search
(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)
array_search β ΠΡΡΡΠ΅ΡΡΠ²Π»ΡΠ΅Ρ ΠΏΠΎΠΈΡΠΊ Π΄Π°Π½Π½ΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΠΈ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΠΊΠ»ΡΡ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Π½Π°ΠΉΠ΄Π΅Π½Π½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅ΡΠ½ΠΎΠ³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ
ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅
Π‘ΠΏΠΈΡΠΎΠΊ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ²
ΠΡΠ»ΠΈ needle ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΡΡΠΎΠΊΠΎΠΉ, ΡΡΠ°Π²Π½Π΅Π½ΠΈΠ΅ ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ Ρ ΡΡΡΡΠΎΠΌ ΡΠ΅Π³ΠΈΡΡΡΠ°.
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ
ΠΡΠΈΠΌΠ΅ΡΡ
ΠΡΠΈΠΌΠ΅Ρ #1 ΠΡΠΈΠΌΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ array_search()
Π‘ΠΌΠΎΡΡΠΈΡΠ΅ ΡΠ°ΠΊΠΆΠ΅
User Contributed Notes 44 notes
in (PHP 5 >= 5.5.0) you don’t have to write your own function to search through a multi dimensional array
$userdb=Array
(
(0) => Array
(
(uid) => ‘100’,
(name) => ‘Sandra Shush’,
(url) => ‘urlof100’
),
(1) => Array
(
(uid) => ‘5465’,
(name) => ‘Stefanie Mcmohn’,
(pic_square) => ‘urlof100’
),
(2) => Array
(
(uid) => ‘40489’,
(name) => ‘Michael’,
(pic_square) => ‘urlof40489’
)
);
simply u can use this
$key = array_search(40489, array_column($userdb, ‘uid’));
About searcing in multi-dimentional arrays; two notes on «xfoxawy at gmail dot com»;
It perfectly searches through multi-dimentional arrays combined with array_column() (min php 5.5.0) but it may not return the values you’d expect.
Secondly, if your array is big, I would recommend you to first assign a new variable so that it wouldn’t call array_column() for each element it searches. For a better performance, you could do;
It’s what the document stated «may also return a non-Boolean value which evaluates to FALSE.»
the recursive function by tony have a small bug. it failes when a key is 0
here is the corrected version of this helpful function:
If you are using the result of array_search in a condition statement, make sure you use the === operator instead of == to test whether or not it found a match. Otherwise, searching through an array with numeric indicies will result in index 0 always getting evaluated as false/null. This nuance cost me a lot of time and sanity, so I hope this helps someone. In case you don’t know what I’m talking about, here’s an example:
hallo every body This function matches two arrays like
search an array like another or not array_match which can match
for searching case insensitive better this:
About searcing in multi-dimentional arrays;
note on «xfoxawy at gmail dot com» and turabgarip at gmail dot com;
$xx = array_column($array, ‘NAME’, ‘ID’);
will produce an array like :
$xx = [
[ID_val] => NAME_val
[ID_val] => NAME_val
]
$yy = array_search(‘tesxt’, array_column($array, ‘NAME’, ‘ID’));
will output expected ID;
To expand on previous comments, here are some examples of
where using array_search within an IF statement can go
wrong when you want to use the array key thats returned.
Take the following two arrays you wish to search:
I was going to complain bitterly about array_search() using zero-based indexes, but then I realized I should be using in_array() instead.
The essence is this: if you really want to know the location of an element in an array, then use array_search, else if you only want to know whether that element exists, then use in_array()
Be careful when search for indexes from array_keys() if you have a mixed associative array it will return both strings and integers resulting in comparison errors
/* The above prints this, as you can see we have mixed keys
array(3) <
[0]=>
int(0)
[1]=>
string(3) «car»
[2]=>
int(1)
>
*/
hey i have a easy multidimensional array search function
Despite PHP’s amazing assortment of array functions and juggling maneuvers, I found myself needing a way to get the FULL array key mapping to a specific value. This function does that, and returns an array of the appropriate keys to get to said (first) value occurrence.
But again, with the above solution, PHP again falls short on how to dynamically access a specific element’s value within the nested array. For that, I wrote a 2nd function to pull the value that was mapped above.
I needed a way to return the value of a single specific key, thus:
Better solution of multidimensional searching.
FYI, remember that strict mode is something that might save you hours.
one thing to be very aware of is that array_search() will fail if the needle is a string and the array itself contains values that are mixture of numbers and strings. (or even a string that looks like a number)
The problem is that unless you specify «strict» the match is done using == and in that case any string will match a numeric value of zero which is not what you want.
also, php can lookup an index pretty darn fast. for many scenarios, it is practical to maintain multiple arrays, one in which the index of the array is the search key and the normal array that contains the data.
//very fast lookup, this beats any other kind of search
I had an array of arrays and needed to find the key of an element by comparing actual reference.
Beware that even with strict equality (===) php will equate arrays via their elements recursively, not by a simple internal pointer check as with class objects. The === can be slow for massive arrays and also crash if they contain circular references.
This function performs reference sniffing in order to return the key for an element that is exactly a reference of needle.
A simple recursive array_search function :
A variation of previous searches that returns an array of keys that match the given value:
I needed a function, that returns a value by specifying a keymap to the searched value in a multidimensional array and came up with this.
My function get_key_in_array() needed some improvement:
An implementation of a search function that uses a callback, to allow searching for objects of arbitrary complexity:
For instance, if you have an array of objects with an id property, you could search for the object with a specific id like this:
For a more complex example, this function takes an array of key/value pairs and returns the key for the first item in the array that has all those properties with the same values.
The final step is a function that returns the item, rather than its key, or null if no match found:
Π€ΡΠ½ΠΊΡΠΈΠΈ Π΄Π»Ρ ΡΠ°Π±ΠΎΡΡ Ρ ΠΌΠ°ΡΡΠΈΠ²Π°ΠΌΠΈ
Π‘ΠΎΠ΄Π΅ΡΠΆΠ°Π½ΠΈΠ΅
User Contributed Notes 14 notes
A simple trick that can help you to guess what diff/intersect or sort function does by name.
Example: array_diff_assoc, array_intersect_assoc.
Example: array_diff_key, array_intersect_key.
Example: array_diff, array_intersect.
Example: array_udiff_uassoc, array_uintersect_assoc.
This also works with array sort functions:
Example: arsort, asort.
Example: uksort, ksort.
Example: rsort, krsort.
Example: usort, uasort.
?>
Return:
Array ( [ 0 ] => Cero [ 1 ] => Uno [ 2 ] => Dos [ 3 ] => Cuatro [ 4 ] => Cinco [ 5 ] => Tres [ 6 ] => Seis [ 7 ] => Siete [ 8 ] => Ocho [ 9 ] => Nueve [ 10 ] => Diez )
Array ( [ 0 ] => Cero [ 1 ] => Uno [ 2 ] => Dos [ 3 ] => Tres [ 4 ] => Cuatro [ 5 ] => Cinco [ 6 ] => Seis [ 7 ] => Siete [ 8 ] => Ocho [ 9 ] => Nueve [ 10 ] => Diez )
?>
Updated code of ‘indioeuropeo’ with option to input string-based keys.
Here is a function to find out the maximum depth of a multidimensional array.
// return depth of given array
// if Array is a string ArrayDepth() will return 0
// usage: int ArrayDepth(array Array)
Short function for making a recursive array copy while cloning objects on the way.
If you need to flattern two-dismensional array with single values assoc subarrays, you could use this function:
to 2g4wx3:
i think better way for this is using JSON, if you have such module in your PHP. See json.org.
to convert JS array to JSON string: arr.toJSONString();
to convert JSON string to PHP array: json_decode($jsonString);
You can also stringify objects, numbers, etc.
Function to pretty print arrays and objects. Detects object recursion and allows setting a maximum depth. Based on arraytostring and u_print_r from the print_r function notes. Should be called like so:
I was looking for an array aggregation function here and ended up writing this one.
Note: This implementation assumes that none of the fields you’re aggregating on contain The ‘@’ symbol.
While PHP has well over three-score array functions, array_rotate is strangely missing as of PHP 5.3. Searching online offered several solutions, but the ones I found have defects such as inefficiently looping through the array or ignoring keys.
Website-create.ru
ΠΠ°ΡΠ°ΡΡΡΡ ΠΏΡΠΈ Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΠΈ ΠΊΠΎΠ΄Π° Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ Π»ΠΈ ΡΠΎ ΠΈΠ»ΠΈ ΠΈΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π² ΠΌΠ°ΡΡΠΈΠ²Π΅. Π‘Π΅Π³ΠΎΠ΄Π½Ρ ΠΌΡ ΡΠ°ΡΡΠΌΠΎΡΡΠΈΠΌ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΡΠ½ΠΊΡΠΈΠΉ, ΠΏΡΠΈ ΠΏΠΎΠΌΠΎΡΠΈ ΠΊΠΎΡΠΎΡΡΡ ΡΡΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π°Π»ΠΈΡΠΈΡ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠΌΠ΅Π½ΡΡΡΡΡ ΠΏΡΠΈ ΡΠ΅ΡΠ΅Π½ΠΈΠΈ ΡΠ°Π·Π»ΠΈΡΠ½ΡΡ Π·Π°Π΄Π°Ρ Π² ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠΈ.
ΠΡ ΠΌΠΎΠΆΠ΅ΠΌ ΠΏΠΎΠ»ΡΡΠ°ΡΡ ΡΠ°Π·Π»ΠΈΡΠ½ΡΠ΅ ΠΌΠ°ΡΡΠΈΠ²Ρ ΠΈΠ· Π½Π°ΡΠ΅ΠΉ Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ ΠΈ ΠΏΡΠΎΠ²Π΅ΡΡΡΡ Π½Π°Π»ΠΈΡΠΈΠ΅ ΡΠΎΠ³ΠΎ ΠΈΠ»ΠΈ ΠΈΠ½ΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² Π½Π΅ΠΌ. ΠΡΠΊΠΎΠΌΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π²Π°ΡΡΡΡ ΠΈ ΠΎΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ Π½Π°ΡΠ΅Π³ΠΎ ΡΠΊΡΠΈΠΏΡΠ°, ΠΊΠΎΠ³Π΄Π° ΠΎΠ½, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, ΡΡΠΎ-ΡΠΎ ΠΈΡΠ΅Ρ. ΠΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°ΠΌ ΡΠ°ΠΊΠΎΠ³ΠΎ ΠΏΠΎΠΈΡΠΊΠ° ΠΌΠΎΠΆΠ½ΠΎ ΡΠΎΠ²Π΅ΡΡΠ°ΡΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Π½ΡΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΡ. ΠΡΠ΅ Π·Π°Π²ΠΈΡΠΈΡ ΠΎΡ ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΠΎ ΠΏΠΎΡΡΠ°Π²Π»Π΅Π½Π½ΠΎΠΉ Π·Π°Π΄Π°ΡΠΈ, ΠΎΠ΄Π½Π°ΠΊΠΎ, Π°Π»Π³ΠΎΡΠΈΡΠΌΡ ΠΏΠΎΠΈΡΠΊΠ° Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ Π±ΡΠ΄ΡΡ ΠΎΠ΄Π½ΠΈΠΌΠΈ ΠΈ ΡΠ΅ΠΌΠΈ ΠΆΠ΅.
Π‘Π΅Π³ΠΎΠ΄Π½Ρ ΠΌΡ ΠΈΡ ΡΠ°ΡΡΠΌΠΎΡΡΠΈΠΌ.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π°Π»ΠΈΡΠΈΡ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅. Π€ΡΠ½ΠΊΡΠΈΡ in_array()
Π€ΡΠ½ΠΊΡΠΈΡ in_array() ΠΏΠΎΠ·Π²ΠΎΠ»ΠΈΡ Π½Π°ΠΌ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π°Π»ΠΈΡΠΈΠ΅ ΠΊΠ°ΠΊΠΎΠ³ΠΎ-Π»ΠΈΠ±ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅.
ΠΡΠ»ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π΅Π΅ ΡΠ°Π±ΠΎΡΡ ΡΠ΄Π°ΡΠ½ΡΠΉ ΠΈ ΠΈΡΠΊΠΎΠΌΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ Π½Π°ΠΉΠ΄Π΅Π½, ΡΠΎ ΡΡΠ½ΠΊΡΠΈΡ Π²Π΅ΡΠ½Π΅Ρ true, ΡΠΎ Π΅ΡΡΡ Β«ΠΏΡΠ°Π²Π΄ΡΒ».
Π’Π°ΠΊΠΆΠ΅ Π½ΡΠΆΠ½ΠΎ ΠΏΠΎΠΌΠ½ΠΈΡΡ, ΡΡΠΎ ΡΡΠ½ΠΊΡΠΈΡ ΠΎΡΡΡΠ΅ΡΡΠ²Π»ΡΠ΅Ρ ΡΡΠ°Π²Π½Π΅Π½ΠΈΠ΅ Ρ ΡΡΠ΅ΡΠΎΠΌ ΡΠ΅Π³ΠΈΡΡΡΠ° ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ².
ΠΠ°Π²Π°ΠΉΡΠ΅ ΡΠ°ΡΡΠΌΠΎΡΡΠΈΠΌ ΡΠ°Π±ΠΎΡΡ ΡΡΠΎΠΉ ΡΡΠ½ΠΊΡΠΈΠΈ Π½Π° ΠΏΡΠΎΡΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅.
ΠΠ°ΠΌ Π½ΡΠΆΠ΅Π½ ΠΊΠ°ΠΊΠΎΠΉ-Π½ΠΈΠ±ΡΠ΄Ρ ΠΌΠ°ΡΡΠΈΠ². ΠΡΠΈ ΠΏΠΎΠΌΠΎΡΠΈ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΏΡΠΎΠ²Π΅ΡΠΈΠΌ Π½Π°Π»ΠΈΡΠΈΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΠΈ Π²ΡΠ²Π΅Π΄Π΅ΠΌ Π½Π° ΡΠΊΡΠ°Π½ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅.
ΠΡΡΠ°Π±ΠΎΡΠ°Π² ΡΡΠ½ΠΊΡΠΈΡ Π²ΡΠ²Π΅Π΄Π΅Ρ Π½Π° ΡΠΊΡΠ°Π½ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Β«YesΒ», ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΡΠ»Π΅ΠΌΠ΅Π½Ρ Β«MarinaΒ» Π² Π½Π°ΡΠ΅ΠΌ ΠΌΠ°ΡΡΠΈΠ²Π΅ ΠΏΡΠΈΡΡΡΡΡΠ²ΡΠ΅Ρ.
ΠΠΎΠΌΠ΅Π½ΡΠΉΡΠ΅ ΠΏΠ΅ΡΠ²ΡΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ Π² ΡΡΠ½ΠΊΡΠΈΠΈ Π½Π° ΠΊΠ°ΠΊΠΎΠΉ-Π»ΠΈΠ±ΠΎ Π½Π΅ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠΈΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ, ΠΈ ΠΡ ΡΠ²ΠΈΠ΄ΠΈΡΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Β«NoΒ».
ΠΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π°Π»ΠΈΡΠΈΡ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅. Π€ΡΠ½ΠΊΡΠΈΡ array_search()
Π‘ΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ ΠΈ Π΅ΡΠ΅ ΠΎΠ΄Π½Π° ΡΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΠΏΠΎΠΈΡΠΊΠ° array_search(), ΠΊΠΎΡΠΎΡΠ°Ρ Π² ΠΎΡΠ»ΠΈΡΠΈΠ΅ ΠΎΡ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠ΅ΠΉ Π±ΡΠ΄Π΅Ρ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΊΠ»ΡΡ Π½Π°ΠΉΠ΄Π΅Π½Π½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°. ΠΡΠΎ Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ³ΠΎΠ΄ΠΈΡΡΡΡ, Π΅ΡΠ»ΠΈ ΠΌΡ ΡΠ°Π±ΠΎΡΠ°Π΅ΠΌ Ρ Π°ΡΡΠΎΡΠΈΠ°ΡΠΈΠ²Π½ΡΠΌ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ.
Π€ΡΠ½ΠΊΡΠΈΡ ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΡΠ΅ ΠΆΠ΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ, ΡΡΠΎ ΠΈ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠ°Ρ. ΠΡΠΈ ΡΡΠΎΠΌ ΡΡΠ΅ΡΠΈΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ ΡΠ°ΠΊΠΆΠ΅ ΡΠ²Π»ΡΠ΅ΡΡΡ Π½Π΅ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΡΠΌ.
ΠΠ°Π²Π°ΠΉΡΠ΅ ΠΏΠΎΡΠΌΠΎΡΡΠΈΠΌ, ΠΊΠ°ΠΊ Π΅Π΅ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ, ΡΠ°Π±ΠΎΡΠ°Ρ Ρ Π°ΡΡΠΎΡΠΈΠ°ΡΠΈΠ²Π½ΡΠΌ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ.
Π Π΄Π°Π½Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΠΌΡ ΡΠ²ΠΈΠ΄ΠΈΠΌ Π½Π° ΡΠΊΡΠ°Π½Π΅ Β«nameΒ», ΡΠΎ Π΅ΡΡΡ ΠΊΠ»ΡΡ ΠΎΡ ΠΈΡΠΊΠΎΠΌΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΡΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ΠΌ Β«MilaΒ».
ΠΡΠΈ Π΄Π²Π΅ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΎΡΠ΅Π½Ρ ΠΏΠΎΡ ΠΎΠΆΠΈ ΠΈ ΠΏΠΎ ΡΡΡΠΈ ΠΎΡΠ»ΠΈΡΠ°ΡΡΡΡ ΡΠΎΠ»ΡΠΊΠΎ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ΠΌ.
ΠΠΎΠΈΡΠΊ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΠ½ΠΎΠΌ ΠΌΠ°ΡΡΠΈΠ²Π΅
Π ΡΡΠΎ Π΄Π΅Π»Π°ΡΡ, Π΅ΡΠ»ΠΈ ΠΌΡ ΡΠ°Π±ΠΎΡΠ°Π΅ΠΌ Ρ ΠΌΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΠ½ΡΠΌ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ? ΠΠ΅Π΄Ρ Π΅Π³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°ΠΌΠΈ Π±ΡΠ΄ΡΡ Π΄ΡΡΠ³ΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²Ρ.
ΠΠ΄Π΅ΡΡ ΡΠΆΠ΅ ΡΠ°ΡΡΠΌΠΎΡΡΠ΅Π½Π½ΡΠ΅ Π½Π°ΠΌΠΈ Π°Π»Π³ΠΎΡΠΈΡΠΌΡ Π½Π΅ ΡΡΠ°Π±ΠΎΡΠ°ΡΡ.
ΠΠ° ΡΠ°ΠΌΠΎΠΌ Π΄Π΅Π»Π΅ Π²ΡΠ΅ Π½Π΅ ΡΠ°ΠΊ ΡΠΆ ΠΈ ΡΠ»ΠΎΠΆΠ½ΠΎ, ΠΏΡΠΎΡΡΠΎ Π½ΡΠΆΠ½ΠΎ Π½Π΅ΠΌΠ½ΠΎΠ³ΠΎ ΡΡΠ»ΠΎΠΆΠ½ΠΈΡΡ Π²Π΅ΡΡ ΠΌΠ΅Ρ Π°Π½ΠΈΠ·ΠΌ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΈΠΊΠ», Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, foreach(), ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΡΠ΅ΠΊΡΠ°ΡΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ Ρ ΠΌΠ°ΡΡΠΈΠ²Π°ΠΌΠΈ.
ΠΠΎΠΏΡΡΡΠΈΠΌ Ρ Π½Π°Ρ Π΅ΡΡΡ ΠΌΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΠ½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ². ΠΠ³ΠΎ Π½Π΅ΠΏΠΎΡΡΠ΅Π΄ΡΡΠ²Π΅Π½Π½ΡΠΌΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΡΠΌΠΈ ΡΠ²Π»ΡΡΡΡΡ Π΄ΡΡΠ³ΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²Ρ, Π² ΠΊΠΎΡΠΎΡΡΡ ΠΌΠΎΠΆΠ΅Ρ ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΡΡΡ ΠΈΡΠΊΠΎΠΌΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°.
ΠΡΠ΅, ΡΡΠΎ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΠ΄Π΅Π»Π°ΡΡ β ΡΡΠΎ ΠΏΠ΅ΡΠ΅Π±ΡΠ°ΡΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ ΠΏΠ΅ΡΠ²ΠΎΠ½Π°ΡΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΠΌΠ°ΡΡΠΈΠ²Π° Π² ΡΠΈΠΊΠ»Π΅ foreach(). ΠΠ°ΠΆΠ΄ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΡΠΎΠ³ΠΎ ΠΌΠ°ΡΡΠΈΠ²Π° Π±ΡΠ΄Π΅Ρ ΡΠ°Π·ΠΎΠ±ΡΠ°Π½ Π½Π° ΠΊΠ»ΡΡ ($key) ΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ($value).
ΠΠ½Π°ΡΠ΅Π½ΠΈΠ΅ΠΌ Π±ΡΠ΄Π΅Ρ ΡΠ²Π»ΡΡΡΡΡ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· ΠΌΠ°ΡΡΠΈΠ²ΠΎΠ², Π½Π°Ρ ΠΎΠ΄ΡΡΠΈΠΉΡΡ Π²Π½ΡΡΡΠΈ ΠΎΡΠ½ΠΎΠ²Π½ΠΎΠ³ΠΎ ΠΌΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΠ½ΠΎΠ³ΠΎ ΠΌΠ°ΡΡΠΈΠ²Π°. ΠΠΎΡ Ρ ΡΡΠΈΠΌΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΡΠΌΠΈ ΠΌΡ ΠΈ Π±ΡΠ΄Π΅ΠΌ ΡΠ°Π±ΠΎΡΠ°ΡΡ, ΠΈΡΠ° Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ Π²Π½ΡΡΡΠ΅Π½Π½Π΅ΠΌ ΠΌΠ°ΡΡΠΈΠ²Π΅ ΠΈΡΠΊΠΎΠΌΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°.
ΠΡΠΈ Π½Π°Ρ ΠΎΠΆΠ΄Π΅Π½ΠΈΠΈ ΠΌΡ Π²ΡΠ²Π΅Π΄Π΅ΠΌ Π½Π° ΡΠΊΡΠ°Π½ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΎ ΡΠΎΠΌ, ΡΡΠΎ ΡΠ°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ, Π° Π΅ΡΠ»ΠΈ Π½Π΅Ρ, ΡΠΎ Π²ΡΠ²Π΅Π΄Π΅ΠΌ Π΄ΡΡΠ³ΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅, ΡΡΠΎ ΡΠ°ΠΊΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π½Π΅Ρ.
ΠΠ°Π²Π°ΠΉΡΠ΅ ΠΏΠΎΡΠΌΠΎΡΡΠΈΠΌ Π²ΡΠ΅ ΡΡΠΎ Π½Π° ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΊΠΎΠ΄Π°:
ΠΠ°ΠΊ ΠΡ Π²ΠΈΠ΄ΠΈΡΠ΅, Π²Π½Π°ΡΠ°Π»Π΅ ΠΌΡ ΠΎΠ±ΡΡΠ²Π»ΡΠ΅ΠΌ ΡΠ°ΠΌ ΠΌΠ½ΠΎΠ³ΠΎΠΌΠ΅ΡΠ½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ².
ΠΡΠΈ ΡΡΠΎΠΌ Π·Π΄Π΅ΡΡ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ Π½ΡΠΆΠ½ΠΎ ΠΏΠΈΡΠ°ΡΡ Π½Π΅ ΠΏΡΠΎΡΡΠΎ Π·Π½Π°ΠΊ ΡΠ°Π²Π΅Π½ΡΡΠ²Π°, Π° Β«.=Β».
ΠΠ°ΠΊ ΠΡ ΠΏΠΎΠ½ΡΠ»ΠΈ, ΠΈΡΠΎΠ³ΠΎΠΌ ΡΠ°Π±ΠΎΡΡ ΡΡΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π° Π±ΡΠ΄Π΅Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Β«OK! Element here!Β».
ΠΠΎΠΏΡΠΎΠ±ΡΠΉΡΠ΅ ΠΏΠΎΠΌΠ΅Π½ΡΡΡ ΠΈΡΠΊΠΎΠΌΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ Π½Π° Π½Π΅ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠΈΠΉ ΠΈ ΠΡ ΡΠ²ΠΈΠ΄ΠΈΡΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Β«No have element!Β».
ΠΠΎΠ½Π΅ΡΠ½ΠΎ ΠΆΠ΅, ΠΏΡΠΈ Π½Π°Ρ ΠΎΠΆΠ΄Π΅Π½ΠΈΠΈ ΠΈΠ»ΠΈ Π½Π΅ Π½Π°Ρ ΠΎΠΆΠ΄Π΅Π½ΠΈΠΈ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΡ ΠΌΠΎΠΆΠ΅ΠΌ Π½Π΅ ΠΏΡΠΎΡΡΠΎ Π²ΡΠ²ΠΎΠ΄ΠΈΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ, Π° Π΄Π΅Π»Π°ΡΡ ΠΊΠ°ΠΊΠΈΠ΅-Π»ΠΈΠ±ΠΎ Π΄ΡΡΠ³ΠΈΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΡ. ΠΡΠ΅ Π·Π°Π²ΠΈΡΠΈΡ ΠΎΡ ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΠΠ°ΠΌ Π½ΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ. ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΏΡΠΈ Π½Π°Π»ΠΈΡΠΈΠΈ ΠΈΡΠΊΠΎΠΌΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅, ΠΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ ΠΎΡΠ΄Π°Π²Π°ΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ ΠΊΠ°ΠΊΡΡ-ΡΠΎ ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΈ Ρ.Π΄.
ΠΠΎΡ ΠΈ Π²ΡΠ΅ Π½Π° ΡΠ΅Π³ΠΎΠ΄Π½Ρ! ΠΠ°Π΄Π΅ΡΡΡ, ΡΡΠΎΠΊ Π±ΡΠ» ΠΏΠΎΠ½ΡΡΠ΅Π½ ΠΈ ΠΏΠΎΠ»Π΅Π·Π΅Π½! ΠΠΎΠΏΡΠΎΠ±ΡΠΉΡΠ΅ ΡΠ°ΠΌΠΈ Π½Π°ΠΏΠΈΡΠ°ΡΡ ΠΏΠΎΠ΄ΠΎΠ±Π½ΡΠΉ ΠΊΠΎΠ΄, ΡΡΠΎΠ±Ρ ΡΠ°Π·ΠΎΠ±ΡΠ°ΡΡΡΡ Π²ΠΎ Π²ΡΠ΅ΠΌ ΠΎΠΊΠΎΠ½ΡΠ°ΡΠ΅Π»ΡΠ½ΠΎ.
Π Ρ ΠΆΠ΄Ρ ΠΠ°ΡΠΈΡ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠ΅Π².
ΠΠ΅Π»ΠΈΡΠ΅ΡΡ ΡΡΠΎΠΊΠΎΠΌ ΡΠΎ ΡΠ²ΠΎΠΈΠΌΠΈ Π΄ΡΡΠ·ΡΡΠΌΠΈ ΠΏΡΠΈ ΠΏΠΎΠΌΠΎΡΠΈ ΠΊΠ½ΠΎΠΏΠΎΠΊ ΡΠΎΡ. ΡΠ΅ΡΠ΅ΠΉ, ΡΠ°ΡΠΏΠΎΠ»ΠΎΠΆΠ΅Π½Π½ΡΡ Π½ΠΈΠΆΠ΅. Π ΡΠ°ΠΊΠΆΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΡΠ²Π°ΠΉΡΠ΅ΡΡ Π½Π° ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΡ Π±Π»ΠΎΠ³Π°. ΠΡ ΡΠΆΠ΅ ΡΠΎΠ±ΡΠ°Π»ΠΈ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ Π½Π΅ΠΏΠ»ΠΎΡ ΠΎΠΉ Π°ΡΡ ΠΈΠ² ΠΏΠΎΠ»Π΅Π·Π½ΡΡ ΠΌΠ°ΡΠ΅ΡΠΈΠ°Π»ΠΎΠ², ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΠΏΠΎΠ»Π½ΡΡΡΡΡ!