Array search, with multiple key x value relationships

0

Example scenario

I have the following Array :

array (
  0 => 
  array (
    'A' => 'X',
    'B' => 'Y',
    'C' => 1,
  ),
  1 => 
  array (
    'A' => 'X',
    'B' => 'Y',
    'C' => 2,
  ),
  2 => 
  array (
    'A' => 'X',
    'B' => 'W',
    'C' => 1,
  ),
  3 => 
  array (
    'A' => 'X',
    'B' => 'W',
    'C' => 3,
  ),
)

Objective

I would like to search if the array contains a array , which matches the 3 specific values,

A = X
B = Y
C = 2
# Encontrará o array [1]

A = X
B = Y
C = 3
# Não encontrará nenhum valor

Doubt

  • What is the best way to search?
asked by anonymous 06.12.2018 / 20:09

2 answers

5

You just build a array similar to what's on the whitelist and use the array_search function:

$A = 'X';
$B = 'Y';
$C = 2;

$indice = array_search(compact('A', 'B', 'C'), $permissoes);

The compact function will generate a array ['A' => 'X', 'B' => 'Y', 'C' => 2] , searching for this array in the list of permissions. If it finds, the index of the first occurrence is returned; otherwise it will return false .

$A = 'X';
$B = 'Y';
$C = 2;

if (($indice = array_search(compact('A', 'B', 'C'), $permissoes)) !== false) {
    echo "Permissões encontradas no índice {$indice}";
} else {
    echo "Permissões não encontradas";
}

See working at Repl.it | Ideone

    
06.12.2018 / 20:18
5

You can use the array_search function to search for values within the array . But if you do not need to know where the element is, the in_array function serves the purpose.

The behavior of comparisons between arrays will depend on whether the comparison is strict or not . Where the normal comparison checks if the arrays have the same key / values pairs and the restricted comparison also checks if they are in the same order. Ex.:

$a = ['a' => 1, 'b' => 2];
$b = ['b' => 2, 'a' => 1];

$a == $b;  // true
$a === $b; // false

That said, just use in_array :

<?php

$permissoes = [
    ["A" => "X", "B" => "Y", "C" => 1],
    ["A" => "X", "B" => "Y", "C" => 2],
    ["A" => "X", "B" => "W", "C" => 1],
    ["A" => "X", "B" => "W", "C" => 3],
];

$existente = ["A" => "X", "B" => "Y", "C" => 2];
$inexistente = ["A" => "X", "B" => "Y", "C" => 3];

// Printa: "Tem permissão"
if (in_array($existente, $permissoes)) {
    echo "Tem permissão";
} else {
    echo "Não tem permissão";
}

// Printa: "Não tem permissão"
if (in_array($inexistente, $permissoes)) {
    echo "Tem permissão";
} else {
    echo "Não tem permissão";
}

Repl.it with the working code

It is important to remember that the array_search method returns the index of the element found in the array or false if the element is not found. So remember to make a narrow comparison, because if the element is in the first position the result will be zero, which is a falsy value. (If you use in_array this is not a problem). Ex:

<?php

$a = [1, 2, 3, 4];

$result = array_search(1, $a);

// Printa: "Não encontrado"
if ($result) {
    echo "Encontrado";
} else {
    echo "Não encontrado";
}
    
06.12.2018 / 20:29