Home php PHP Element search in two-dimensional array

PHP Element search in two-dimensional array

Author

Date

Category

There is a variable $ A with a value of 5 or 3. or there is a variable $ b with some value.

How to learn without busting in the cycle, i.e. Without third-party functions, whether the value of the variable $ A coincides with any element of the $ ARR array over the ‘A’ field. The same with the variable $ B? Something like in_array () or array_search ().


Answer 1, Authority 100%

You can use the Array_Column to get an array of the values ​​of the desired speaker For example

$ a = 3;
if (in_array ($ a, array_column ($ arr, 'a'))) {
  Echo 'Yes';
}

Answer 2

Option for PHP & LT; = 5.4

$ arr = array (
  array ('a' = & gt; 2, 'b' = & gt; 15),
  array ('a' = & gt; 3, 'b' = & gt; 15),
  array ('a' = & gt; 4, 'b' = & gt; 2),
  array ('a' = & gt; 8, 'b' = & gt; 3),
  array ('a' = & gt; 15, 'b' = & gt; 1),
);
// The desired value
$ srch = 15;
Array_Filter ($ ARR, FUNCTION ($ A) USE (& amp; $ int, $ srch) {
  Return (Array_Search ($ srch, $ a))? ++ $ int: false;
});
// Number of coincidences
Echo ($ int? $ int: 0); // 3

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions