subhra subhra - 4 months ago 25
PHP Question

How to search the key value is exist in array using PHP

I need one help. I have two array and i need to search first array value is present in second or not. I am explaining the array below.


Here i need if each value from first array is present inside second array or not. If yes then it will return
and if no
at each time.


Here you go, you can use the in_array() for PHP.

$maindata=array( array('id'=>3),array('id'=>7),array('id'=>9) );
$childata=array( array('id'=>7),array('id'=>11),array('id'=>3),array('id'=>123) );

foreach( $maindata as $key => $value )
  if( in_array( $value, $childata ) )
    echo true;
    echo false;

You could also remove the whole if else and replace with a single line.

echo ( in_array( $value, $childata ) ? true : false );

Reference -