array_key_exists:: Controleren op alle bestaande sleutels
Oplossing met foreach
Code (php)
Gewijzigd op 24/11/2014 20:11:56 door G P
Het werkt maar weet niet of dit wel de beste oplossing is.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
function check_keys($checkArray = array(), $keys = array()){
// Controleer op bestaande arrays
if (is_array($checkArray) && is_array($keys)){
$keys = array_flip($keys); // Wissel key en value
$result['keys_exists'] = array();
$result['keys_not_exists'] = array();
$result['keys_other'] = array();
// Controleer op alle keys aanwezig
if (count(array_intersect_key($keys, $checkArray)) !== count($keys)){
$result['keys_not_exists'] = array_diff_key($keys, $checkArray);
}
$result['keys_exists'] = array_intersect_key($keys, $checkArray); // Toon bestaande keys
$result['keys_other'] = array_diff_key($checkArray, $keys); // Toon niet aangegeven keys
$result = array_map('array_flip', $result); // Wissel key en inhoud
// Sorteren
sort($result['keys_exists']);
sort($result['keys_not_exists']);
sort($result['keys_other']);
// Terug sturen van resultaten
return $result;
}
}
// Test
// Maak een array
$checkArray = array(
'key1' => 1,
'key2' => 2,
'key3' => 3,
'key4' => 4,
'key5' => 5
);
// Verplichte keys
$keys = array('key1', 'key6');
// Controleer bestaande keys met functie
$check = check_keys($checkArray, $keys);
// Toon resultaten
print '<pre>';
print_r($check);
print '</pre>';
?>
function check_keys($checkArray = array(), $keys = array()){
// Controleer op bestaande arrays
if (is_array($checkArray) && is_array($keys)){
$keys = array_flip($keys); // Wissel key en value
$result['keys_exists'] = array();
$result['keys_not_exists'] = array();
$result['keys_other'] = array();
// Controleer op alle keys aanwezig
if (count(array_intersect_key($keys, $checkArray)) !== count($keys)){
$result['keys_not_exists'] = array_diff_key($keys, $checkArray);
}
$result['keys_exists'] = array_intersect_key($keys, $checkArray); // Toon bestaande keys
$result['keys_other'] = array_diff_key($checkArray, $keys); // Toon niet aangegeven keys
$result = array_map('array_flip', $result); // Wissel key en inhoud
// Sorteren
sort($result['keys_exists']);
sort($result['keys_not_exists']);
sort($result['keys_other']);
// Terug sturen van resultaten
return $result;
}
}
// Test
// Maak een array
$checkArray = array(
'key1' => 1,
'key2' => 2,
'key3' => 3,
'key4' => 4,
'key5' => 5
);
// Verplichte keys
$keys = array('key1', 'key6');
// Controleer bestaande keys met functie
$check = check_keys($checkArray, $keys);
// Toon resultaten
print '<pre>';
print_r($check);
print '</pre>';
?>
Met deze functie toont het op bestaande keys, niet aanwezige keys, en overige keys.
De output van dit:
Array
(
[keys_exists] => Array
(
[0] => key1
)
[keys_not_exists] => Array
(
[0] => key6
)
[keys_other] => Array
(
[0] => key2
[1] => key3
[2] => key4
[3] => key5
)
)
Dat je wilt controleren op verplichte keys dat begrijp ik nog wel. Maar verder houd het voor mij op. Zie niet in waarom je een array van arraykeys die 'bestaan' wilt gaan maken, want die heb je al in je originele array.
ooit gehoord van de functie array_keys()?
Gewijzigd op 24/11/2014 22:28:24 door Frank Nietbelangrijk
Foutmelding 1) Nodige sleutels niet gevonden
Foutmelding 2) Volgende sleutel(s) niet gevonden: key6
De tweede vind ik toch beter dan de eerste... Niet?
Code (php)
1
2
3
4
5
2
3
4
5
<?php
$data = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
$keys_needed = array('key1', 'key5', 'key3');
$missing_keys = array_diff_key(array_flip($keys_needed), $data);
?>
$data = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
$keys_needed = array('key1', 'key5', 'key3');
$missing_keys = array_diff_key(array_flip($keys_needed), $data);
?>