-2

Is it possible to check if an array contains key with "Path" word in it and return its value without using a foreach loop?

For example here I want to have the value2 returned:

$array = [];
$array['abc'] = "value1";
$array['abc_Path'] = "value2";
divHelper11
  • 2,090
  • 3
  • 22
  • 37
  • Although it still uses a loop internally - https://stackoverflow.com/questions/4260086/php-how-to-use-array-filter-to-filter-array-keys – Nigel Ren Sep 14 '20 at 16:50
  • [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). There's a few examples on SO of this being used, see comment above. – Lewis Sep 14 '20 at 16:50

1 Answers1

0
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;

Or use array_map with your own function to figure out.

Rodrigo Guariento
  • 137
  • 1
  • 1
  • 13