-2

how to check whether 4 exist or not in array at id key position

$arr = array(
    array(
        'id' => 1,
        'other_data' => 'ganesh'
       ),
    array(
        'id' => 2,
        'other_data' => 'ramesh'
       ),
    array(
        'id' => 3,
        'other_data' => '4'
       ),
)
Junius L
  • 15,881
  • 6
  • 52
  • 96
VBMali
  • 1,360
  • 3
  • 19
  • 46
  • 2
    Possible duplicate of [How to search by key=>value in a multidimensional array in PHP](http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php) – Daniel May 17 '17 at 11:57
  • if that's your real code it should give you **Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting** – Masivuye Cokile May 17 '17 at 11:57
  • after each array in the array, add a comma `,` – Rotimi May 17 '17 at 11:58
  • 1
    `in_array(4, array_map(function($v) { return $v['id']; }, $arr))` – MrRP May 17 '17 at 11:58

6 Answers6

2

The array you provided is not a valid multi-dimensional array. You need to add commas after each array in the array. Below i use array_column and in_array without using foreach

<?php
    $arr = array(
        array(
            'id' => 1,
            'other_data' => 'ganesh'
           ),//add comma
        array(
            'id' => 2,
            'other_data' => 'ramesh'
           ),
        array(
            'id' => 3,
            'other_data' => '4'
           ),
    );


    $filtered = array_column($arr, 'id');//return the id column in this array

    if(in_array(4, $filtered)){//check if 4 exists
        echo '4 exists';
    } else {
     echo '4 does not exist';   
    }
    ?>
Rotimi
  • 4,783
  • 4
  • 18
  • 27
1

Quite simple, loop through the array and check if the value exists:

$value = 4;
$exists = false;
foreach($arr as $innerArr){
  if($innerArr['id'] == $value){
    $exists = true;
    break;
  }
}

If $exists is now true, the value exists within the array.

Tobias F.
  • 1,050
  • 1
  • 11
  • 23
1

Try this one, and let me know if you face any problem.

<?php
$arr = array(
        array(
            'id' => 1,
            'other_data' => 'ganesh'
           ),
        array(
            'id' => 2,
            'other_data' => 'ramesh'
           ),
        array(
            'id' => 3,
            'other_data' => '4'
           )
    );

foreach ($arr as $key => $value) {
    if (in_array("4", $value))
      {
        $sub_index = $value['id'];
        echo "Match found at $sub_index";
        break;
      }
}

Just gotta loop through your array and check existence through in_array() function.

1

you need something like this

$arr = array(
        array(
            'id' => 1,
            'other_data' => 'ganesh'
           ),
        array(
            'id' => 2,
            'other_data' => 'ramesh'
           ),
        array(
            'id' => 3,
            'other_data' => '4'
           )
    );
    $exists_flag = false;
    foreach($arr as $inside_arr)
    {
      if($inside_arr['other_data'] == 4) {
        $exists_flag = true;
        break;
      }
    }

    print($exists_flag);

Hope this helps!

zenwraight
  • 2,002
  • 1
  • 10
  • 14
1

As it stand, your array is wrong, you need to separate multi array with commas, you need to not that in_array() will not work with multi array, however you may create a recursive function that will check a provided key does exists or not in an array try code below, hope it helps ,

<?php

$arr = array(
    array(
        'id' => 1,
        'other_data' => 'ganesh'
    ),
    array(
        'id' => 2,
        'other_data' => 'ramesh'
    ),
    array(
        'id' => 3,
        'other_data' => '4'
    )
);

function search_items($search_value, $array)
{
    foreach ($array as $item) {
        if (($item == $search_value) || (is_array($item) && search_items($search_value, $item))) {
            return true;
        }
    }

    return false;
}

echo search_items("4", $arr) ? 'item found' : 'item not found';

?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
1
$result = array_search(4, array_column($arr, 'id'));

If we split this into steps then it would be the following:

$allColumnsNamedId = array_column($arr, 'id'); // find all columns with key 'id'
$resultBoolean = array_search(4, $allColumnsNamedId); //search the array for value 4

if($resultBoolean) {
    echo 'Exists';   
} else {
   echo 'Does not exist';
}
Silviu
  • 31
  • 3