PHP if in_array() how to get the key as well?



PHP Snippet 1:

if (false !== $key = array_search(5, $array)) {
    // found!
} else {
    // not found!
}

PHP Snippet 2:

$key = array_search(5, $array);
if ($key !== false) {
    // Found...
}

PHP Snippet 3:

$keys = array_keys($array, 5);
if (count($keys) > 0) {
    // At least one match...
}

PHP Snippet 4:

$key = array_search(5, $array)
if ($key !== false) {
...

PHP Snippet 5:

if(in_array(5, $array))
{
    $key = array_search(5, $array);
    echo $key;
}

PHP Snippet 6:

$myArray =

array:3 [?
  0 => array:3 [?
    0 => 2
    1 => 0
    2 => "2019-07-21 23:59:59"
  ]
  1 => array:3 [?
    0 => 3
    1 => 2
    2 => "2019-07-21 23:59:59"
  ]
  2 => array:3 [?
    0 => 1
    1 => 1
    2 => "2019-07-21 23:59:59"
  ]
]

PHP Snippet 7:

$Array2 = 

Collection {#771 ?
  #items: array:12 [?
    0 => {#1047 ?
      +"id": 2
      +"name": "demografico"
      +"dict_key": "demographic"
      +"component": "Demographic"
      +"country_id": null
      +"created_at": null
      +"updated_at": null
    }
    1 => {#1041 ?}
    2 => {#1040 ?}
    etc...

PHP Snippet 8:

foreach($Array2 as $key => $item)
    if(false !== $key = array_search($item->id, array_column($myArray, 0))
    // Note that $key is overwritten
        <input type="number" class="form-control" id="{!! $item->id !!}" value="{{ $myArray[$key][1] }}">