0

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

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Yes i have the principal key with this code, but i have one last problem, if the $target array have multiple values, not 3 like in the example, for example: ``` $target = array( '0' => Blanco '1' => 32GB '2' => Plastico '3' => 'Other' ); ``` Its for multiple select items and search that in array principal
@Rocket Updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.