3

My Array looks like this:

$arr = Array();
$arr[] = Array("foo", "bar");
$arr[] = Array("test", "hello");

Now I want to check if $arrcontains an Array which contains foo on first position.

Is there any function for this or should I just loop $arr and search through every Array inside it?

Tobias Baumeister
  • 2,027
  • 3
  • 17
  • 34
  • you might be able to use `array_walk` (http://php.net/manual/en/function.array-walk.php), although that's a loop as well – Zathrus Writer Oct 07 '13 at 14:10
  • I think there is no built in PHP function for this task, you may have to run a loop to achieve this. – black_belt Oct 07 '13 at 14:38
  • If you want to check only in the first positions of the sub arrays then run a loop and search only in the first indexes. – black_belt Oct 07 '13 at 14:41
  • You might be able to use a custom function and use PHP's built-in recursion with [`array_walk_recursive`](http://uk1.php.net/array_walk_recursive) – MDEV Oct 07 '13 at 15:38

3 Answers3

1

One nifty little way of doing this would be to use array_reduce – by passing in a function that sums up the values 1 if foo was found, and 0 if not:

$foo_found = array_reduce(
  $arr,
  function ($num_of_hits, $item, $search_for = 'foo') {
    $num_of_hits += $item[0] === $search_for ? 1 : 0;
    return $num_of_hits;
  }
);

$xyz_found = array_reduce(
  $arr,
  function ($num_of_hits, $item, $search_for = 'xyz') {
    $num_of_hits += $item[0] === $search_for ? 1 : 0;
    return $num_of_hits;
  }
);

var_dump($foo_found, $xyz_found);

returns 1 and 0, cause foo is found once, and xyz is found zero times.

CBroe
  • 82,033
  • 9
  • 81
  • 132
0

Your Array:

$arr = Array();
$arr[] = Array("foo", "bar");
$arr[] = Array("foo", "hello");

My Solution:

echo search_in_array("foo",$arr); // This will show you the number of items found

function search_in_array($value, $arr){

  $num = 0;       
  for ($i = 0; $i < count($arr); ) {    
     if($arr[$i][0] == $value) {
            $num++; 
      } 
     $i++;
  }
  return $num ;
}
black_belt
  • 6,110
  • 33
  • 107
  • 179
0

I think there are few different ways to do it. please check the following

$arr_zero = Array();
$arr_zero[] = Array("foo", "bar");
$arr_zero[] = Array("test", "hello");
$arr_zero[] = Array("nal", "rob");
$array_one = array_shift(array_values($arr_zero));
$element = array_shift(array_values($array_one));
if($element=='foo');
echo "its ok";

or

$arr_zero = Array();
$arr_zero[] = Array("foo", "bar");
$arr_zero[] = Array("test", "hello");
$array_one = array_shift(array_values($arr_zero));
$element = reset($array_one);   // First Element's Value
if($element=='foo');
echo "its ok";

or

$arr_zero = Array();
$arr_zero[] = Array("foo", "bar");
$arr_one = $arr_zero[key($arr_zero)];
echo  $arr_one[key($arr_one)];

To get key

$arr_zero = Array();
$arr_zero[] = Array("foo", "bar");
$arr_zero[] = Array("test", "hello");
$array_one = array_shift(array_values($arr_zero));
echo  key($array_one);   // First Element's Key
user2092317
  • 3,016
  • 5
  • 21
  • 34