Minwork Array
  • Minwork Array
  • Common methods
    • has
    • get → getNestedElement
    • set → setNestedElement
    • remove
    • clone
    • getKeysArray
  • Object oriented methods
    • General information
  • Traversing array
    • Finding
    • Iterating
  • Manipulating array
    • Mapping
    • Filtering
    • Grouping
    • Sorting
    • Computations
    • Flattening
  • Validating array
    • check
    • isEmpty
    • isNested
    • isArrayOfArrays
    • isAssoc
    • isUnique
    • isNumeric
    • hasKeys
  • Utility methods
    • pack
    • unpack
    • createMulti
    • forceArray
    • getDepth
    • random
    • shuffle
    • nth
    • getFirstKey
    • getLastKey
    • getFirstValue
    • getLastValue
Powered by GitBook
On this page

Was this helpful?

  1. Traversing array

Finding

find

Definition

Arr::find(array|Iterator|IteratorAggregate $array, callable $condition, string $return = self::FIND_RETURN_VALUE): mixed|mixed[]

Description

Find array (or iterable object) element(s) that match specified condition.

Modes ($return method argument)

Constant name

Description

FIND_RETURN_VALUE

Return first value of an array that match find condition

FIND_RETURN_KEY

Return first key of an array that match find condition

FIND_RETURN_ALL

Return array of all values (preserving original keys) that match find condition

Examples

$array = [
  'a' => 0, 
  'b' => 1, 
  3 => 'c', 
  4 => 5
];


Arr::find($array, 'boolval') -> 1
Arr::find($array, function ($element) {
  return is_string($element);
}) -> 'c'


Arr::find($array, 'boolval', Arr::FIND_RETURN_KEY) -> 'b'
Arr::find($array, function ($element) {
  return is_string($element);
}, Arr::FIND_RETURN_KEY) -> 3


Arr::find($array, 'boolval', Arr::FIND_RETURN_ALL) -> 
[
  'b' => 1, 
  3 => 'c', 
  4 => 5
]

Arr::find($array, function ($element) {
  return is_string($element);
}, Arr::FIND_RETURN_ALL) -> 
[
  3 => 'c',
]

Arr::find($array, function ($element) {
  return is_number($element);
}, Arr::FIND_RETURN_ALL) -> 
[
  'a' => 0,
  'b' => 1,
  4 => 5
]
PreviousGeneral informationNextIterating

Last updated 5 years ago

Was this helpful?