1

I have an array in the form of:

array(2) { [0]=> array(1) { ["positionID"]=> int(100) } [1]=> array(1) { ["positionID"]=> int(102) } }

I want to check 100 or 102 is contained in the array.

I tried code below, but it doesn't work. Can I get some help?

var_dump(in_array(102, $myArray));
3
  • Someone asked a very similar question to yours few hours ago, see this answer: stackoverflow.com/a/63331887/11430357 Commented Aug 10, 2020 at 1:18
  • 1
    See array_column. Commented Aug 10, 2020 at 1:28
  • how it doesn't work ?, what is the output or the error ? Commented Aug 10, 2020 at 2:00

2 Answers 2

0

As @mario pointed out, you're going to want to use something like array_column:

var_dump(in_array(102, array_column($myArray, 'positionID")));

Sign up to request clarification or add additional context in comments.

Comments

0

Use

in_array(102, array_column($myArray, 'positionID'));

in_array checks the direct values of the array. So doing in_array(102, $myArray);, you are checking an integer (102) against the values of the array which are arrays.

Comments

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.