-1

we have to multi-dimensional array and we need to check some value of every element from array 1 to array two on a specific element key.

we can use following code for handle this types of complex problems


<?php
$arr = [
    ["name"=> "Sonu", "id"=>2135],
    ["name"=>"rahul","id"=>5342],
    ["name"=>"rahul","id"=>123]
];
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);
    $index = array_search($arr[1]["id"], array_column($records, 'id'));
    if($index !== false){
    $records[$index]["name"] = $arr[1]["name"];
        print_r($records[$index]);
    } else {
        echo "not match";
    }
    
?>

1 Answers1

-2
$index = array_search($arr[1]["id"], array_column($records, 'id'));
if($index !== false){
  $records[[$index]["name"]] = $arr[1]["name"];
  print_r($records[$index]);
} else {
    echo "not match";
}
khush
  • 55
  • 8