I would like to check if an element from one array already exists in another array. If it exists then I would like to add all elements from the first array to a new associative array and add one additional key-value ([check] => 0/1) to indicate that the element exists or not in another array.
This is a sample code of what I tried.
$first = array("0"=> 111, "1"=>222, "2"=>333, "3"=> 444);
$second = array("0"=> 22, "1"=>234, "2"=> 456);
$final_array = array();
foreach($first as $f)
{
if(in_array($f, $second))
{
$final_array['id'] = $f;
$final_array['check'] = 1;
}
else
{
$final_array['id'] = $f;
$final_array['check'] = 0;
}
}
For some reasons I am only able to add the last element to the $final_array. Can someone tell me what I did wrong?
//Output for $final_array
Array ( [id] => 444 [check] => 0 )
//final output should look like this
$final_array = array("0"=> array("id" => 111, "check" => 0),
"1" => array("id" => 222, "check" => 1),
"2" => array("id" => 333, "check" => 0),
"3" => array("id" => 444, "check" => 0));