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?

4
  • you might be able to use array_walk (php.net/manual/en/function.array-walk.php), although that's a loop as well Commented Oct 7, 2013 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. Commented Oct 7, 2013 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. Commented Oct 7, 2013 at 14:41
  • You might be able to use a custom function and use PHP's built-in recursion with array_walk_recursive Commented Oct 7, 2013 at 15:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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 ;
}

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.