Home php Find a value in multidimensional PHP array

Find a value in multidimensional PHP array

Author

Date

Category

There is an array of rules:

array
(
  [0] = & gt; Array.
    (
      [Value] = & gt; thirty
      [description] = & gt; 30 seconds
    )
  [1] = & gt; Array.
    (
      [Value] = & gt; 45.
      [description] = & gt; 45 seconds
    )
  [2] = & gt; Array.
    (
      [Value] = & gt; 60.
      [description] = & gt; 1 minute
    )
  [3] = & gt; Array.
    (
      [Value] = & gt; 120.
      [description] = & gt; 2 minutes
    )
)

and I get a variable $ rule_value (for example $ rule_value = 60)

and you need to check if there is a number 60 in this array in the VALUE key.

Is it possible to implement it in 1 line without cycles?

Paw to Array_Map, Array_Search

But not yet. Please tell me


Answer 1, Authority 100%

so:

array_filter ($ ARR, FUNCTION ($ A) {
 RETURN $ A ["VALUE"] == 60;
});

Answer 2, Authority 17%

Even I don’t know, try array_walk, array_walk_recursive something from this but it seems to me it’s easier to pass an array once and say to replace the keys to value


Answer 3

If possible, form an array

$ ar = array (
  [30] = & gt; '30 seconds',
  [60] = & gt; '1 minute',
  [120] = & gt; '2 minutes'
)
// Value will become the key of the array.
//Farther:
if (Array_Key_exists ($ Rule_Value, $ Ar)) {
  // Apply the key to the cell.
  echo $ ar [$ rule_value];
}

Answer 4

$ key = array_search ($ rule_value, array_column ($ array, 'value'));

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