How to verify if a value exists in a multidimensional array?

Asked

Viewed 3,029 times

1

I have the following data coming from a bank:

    array(2) {
  [0]=>
  object(stdClass)#28 (6) {
    ["id"]=>
    string(1) "5"
    ["user"]=>
    string(2) "26"
    ["created_date"]=>
    string(19) "2017-09-29 13:53:38"
    ["first_name"]=>
    string(8) "Gabriela"
    ["last_name"]=>
    string(5) "Silva"
    ["comment"]=>
    string(39) "esse é o comentario para o eeo"
  }
  [1]=>
  object(stdClass)#29 (6) {
    ["id"]=>
    string(1) "1"
    ["user"]=>
    string(1) "1"
    ["created_date"]=>
    string(19) "2017-09-29 00:00:00"
    ["first_name"]=>
    string(8) "Vinicius"
    ["last_name"]=>
    string(6) "Aquino"
    ["comment"]=>
    string(13) "helloooooooo!"
  }
}

How can I check whether the "user" 26 exists on that array?

  • Can use a forech or some function of array_

  • I thought about using foreach, but I wonder if there is another option, as you cited an array function, I tested the in_array and it did not work very well

  • If there’s a value what you’ll do with it?

  • Hide a form.

3 answers

8


If you are sure how many dimensions are, in the case there, apparently from a loop:

foreach($array as $row){
   if($row['user'] == '26'){
      return true;
   }
}

You can also use:

foreach($array as $row){
   if (in_array('26', $array)) {
      return true;
   }
}

You can not use direct in_array in the array precisely because it is in another dimension, I saw there that wanted a solution without a loop, da para 'gambiarrar', is not exactly a gambiarra but:

Separate the column, join, and now check if it exists:

$users_cods = array_column($array, 'user');

if(in_array('26',$users_cods)){
    return true;
}

There are dozens of ways to do this, but these are the ones I use.

  • Thanks, better use the first option with loop found simpler.

  • Anthraxisbr, did you notice the fact that the array question has objects? You accessed as if it were a array associative. Does it work? From what I have tested here (https://3v4l.org/5Tjkl), it seems not, or I was given some detail unnoticed?

  • 2

    @Andersoncarloswoss this var dump object is the object of the class not of the array, the time it passes to a variable it becomes accessible, because if it does not pass it does not access the data

2

The reply from @Anthraxisbr already answers, I will leave one more option:

$needle = "26";
array_map(function($array) use ($needle) { return $array['user'] === $needle;}, $array);

2

Another suggestion that I use in my projects. Follow my contribution.

<?php

$array = array(
    "user1" => array(
        "user" => "20",
        "dados" => array(),
    ),
    "user2" => array(
        "user" => "22",
        "dados" => array(),
    ),
);

$id = "20";
$coluna = "user";

echo $key = array_search(
    $id,
    array_filter(
        array_combine(
            array_keys($array),
            array_column(
                $array, $coluna
            )
        )
    )
); // user1

$key = array_search(
    '50', // Gerar error
    array_filter(
        array_combine(
            array_keys($array),
            array_column(
                $array, $coluna
            )
        )
    )
); // bool(false)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.