This commit is contained in:
2024-03-05 13:39:21 +01:00
parent cea9405670
commit 2eb0e06e7b
12 changed files with 306 additions and 7 deletions

View File

@@ -0,0 +1,73 @@
<?php
/***** Explicación *****/
// array_diff: Compara $claves_a_comprobar con $___SESSION y devuelve los valores de $claves_a_comprobar que no estén presentes en $___SESSION (si están todas, devuelve un array vacío)
// array_keys: Devuelve un array con todas las claves de array
// Comprobación básica => array_diff($claves_a_comprobar, array_keys($___SESSION)
// Forma 1 => array_diff($claves_a_comprobar, array_keys($___SESSION)) === []
// Forma 2 => empty(array_diff($claves_a_comprobar, array_keys($___SESSION)))
// Forma 3 => count(array_diff($claves_a_comprobar, array_keys($___SESSION))) === 0
/***** Ejemplo *****/
$___SESSION = [
'cero' => 0,
'uno' => '1',
'dos' => 'dos',
'tres' => 3,
'cuatro' => '4',
];
/*----- Caso 1: Todas las claves existen -----*/
$claves_a_comprobar = ['uno', 'dos', 'tres'];
// Forma 1
if(array_diff($claves_a_comprobar, array_keys($___SESSION)) === []) {
echo 'OK-[]' . PHP_EOL;
}
else {
echo 'NOOOO-[]' . PHP_EOL;
}
// Forma 2
if(empty(array_diff($claves_a_comprobar, array_keys($___SESSION)))) {
echo 'OK-EMPTY' . PHP_EOL;
}
else {
echo 'NOOOO-EMPTY' . PHP_EOL;
}
// Forma 3
if(count(array_diff($claves_a_comprobar, array_keys($___SESSION))) === 0) {
echo 'OK-COUNT' . PHP_EOL;
}
else {
echo 'NOOOO-COUNT' . PHP_EOL;
}
/*----- Caso 2: No existen todas las claves -----*/
$claves_a_comprobar = ['uno', 'two', 'tres'];
// Forma 1
if(array_diff($claves_a_comprobar, array_keys($___SESSION)) === []) {
echo 'OK-[]' . PHP_EOL;
}
else {
echo 'NOOOO-[]' . PHP_EOL;
}
// Forma 2
if(empty(array_diff($claves_a_comprobar, array_keys($___SESSION)))) {
echo 'OK-EMPTY' . PHP_EOL;
}
else {
echo 'NOOOO-EMPTY' . PHP_EOL;
}
// Forma 3
if(count(array_diff($claves_a_comprobar, array_keys($___SESSION))) === 0) {
echo 'OK-COUNT' . PHP_EOL;
}
else {
echo 'NOOOO-COUNT' . PHP_EOL;
}