Rocket
Rocket

Reputation: 57

Find and get array key from multilevel array from other array

i have this array structure, and i want to find and compare with other array that i have.

This is the array that i want search:

array (size=7)
  0 => 
    array (size=9)
      0 => string 'Dorado' (length=6)
      1 => string '64GB' (length=4)
      2 => string 'Plastico' (length=8)
      'vlr' => string '60000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  1 => 
    array (size=9)
      0 => string 'Blanco' (length=6)
      1 => string '32GB' (length=4)
      2 => string 'Plastico' (length=8)
      'vlr' => string '40000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  2 => 
    array (size=9)
      0 => string 'Blanco' (length=6)
      1 => string '64GB' (length=4)
      2 => string 'Madera' (length=6)
      'vlr' => string '60000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  3 => 
    array (size=9)
      0 => string 'Verde' (length=5)
      1 => string '64GB' (length=4)
      2 => string 'Madera' (length=6)
      'vlr' => string '40000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)

An this is the array with search values:

Array
(
    [0] => Blanco
    [1] => 32GB
    [2] => Plastico
)

I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?

PD: the number of search values are the numbers inside main arrays

Upvotes: 0

Views: 40

Answers (1)

Vasilis G.
Vasilis G.

Reputation: 7844

Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:

$target = array(
    '0' => Blanco
    '1' => 32GB
    '2' => Plastico
);

$wantedKey = null;

foreach($elements as $key => $elem){
  if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
      $wantedKey = $key;
      break;
   }
}

echo $wantedKey;

In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:

foreach($elements as $key => $elem){
    $sameValues = true;
    foreach($target as $t_key => $t_value){
        if($elem[$t_key] != $t_value){
            $sameValues = false;
            break;
        }
    }
    if($sameValues){
        $wantedKey = $key;
        break;
    }
}

echo $wantedKey;

Upvotes: 1

Related Questions