1

for example searching names from the below associative array with out using loop.

$data = array(
            array('name'=>'john doe', 'marks'=>23),
            array('name'=>'stevin smith', 'marks'=>76),
            array('name'=>'david warner', 'marks'=>71)
);
iainn
  • 16,826
  • 9
  • 33
  • 40
Afzal
  • 13
  • 2
  • [array_filter()](http://php.net/manual/en/function.array-filter.php) – MonkeyZeus Apr 17 '18 at 13:20
  • 4
    There's no way to use an array and find something inside it without a loop. Whatever approach you take, it will be a loop. Even if you use `array_map`, `array_filter` or similar, those functions use a loop internally. Now, what you can do is restructure the array so that value of `name` is actually the index. Then you can simply do `if(isset($data['david warner']))`. Avoiding loops is simply stupid and I really can't see why you'd want to do it. – N.B. Apr 17 '18 at 13:20
  • @N.B. I doubt OP cares whether the construct/function loops internally. I think they want to avoid explicitly writing a `foreach(){}` loop. – MonkeyZeus Apr 17 '18 at 13:21
  • 1
    Possible duplicate of [How to search by key=>value in a multidimensional array in PHP](https://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php) – Mickaël Leger Apr 17 '18 at 13:25

3 Answers3

4

You can use array_column() with the third parameter to create an associative array. So use this to create an array of marks, indexed by the name column...

$data = array(
    array('name'=>'john doe', 'marks'=>23),
    array('name'=>'stevin smith', 'marks'=>76),
    array('name'=>'david warner', 'marks'=>71));

$marks = array_column($data, 'marks', 'name');

$name = 'john doe';

if ( isset($marks[$name]))  {
    echo $name."=".$marks[$name];
}

As the array is indexed by the name, just use 'isset()'.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

the best way is to use array_column function

$data = array(
   array('name'=>'john doe', 'marks'=>23),
   array('name'=>'stevin smith', 'marks'=>76),
   array('name'=>'david warner', 'marks'=>71));

  $names = array_column($data, 'name');
  $search = 'david warner';
  if(in_array($search, $names))
   echo "Found";
  else
   echo "Not";
Dev Matee
  • 5,525
  • 2
  • 27
  • 33
0

As long as you are evaluating each information of an array, php will use an iterator internally. And that's a good thing given that loops are really cheap.

But if you don't want to explicitly iterate, here is a solution among others :

  $johnDoeExists = array_walk_recursive( $data, function ( $value, $key ) {
      return $key === 'name' && $value === 'john doe';
  } );

array_walk_recursive evaluates each element of the array, nested or not, with the defined callback function.

Or, wait...

If you REALLY don't want to use a loop and write an absolutely awful and unreliable code, here you go.

$johnExists = strpos(json_encode($data), "john doe") !== 0;

No seriously, just loop over your data.

GuCier
  • 6,919
  • 1
  • 29
  • 36