更新关联数组键

I am having trouble updating the values of an multidimensional associative array. The array $people has been generated from another MySQL call. With : array_push($people, array("forename" => $pat_f_name, "surname" => $pat_s_name,"id" => $id)); I have set $forname and $surname to "", just to have the key there.

I am trying to iterate though the array making an SQL call and retrieve the relevant forename and surname and then update the keys at that index of the array.

Below is my attempt so far.

Thanks in advance.

$stmt = $mysql->stmt_init();

foreach ($people as $person)
{
    if($stmt->prepare("SELECT forename,surname FROM worker WHERE id = ?"))
    {
        $stmt->bind_param('i', $p_id);
        $p_id = $person["id"];
        $stmt->execute();
        $stmt->bind_result($f_name, $s_name);
        while($stmt->fetch())
        {
            $people[$person]["forename"] = $f_name;
            $people[$person]["surname"] = $s_name;

        }
    }
}    


$stmt->close();

$person is an array

$p_id = $person["id"];

this

$people[$person]["forename"] = $f_name;

should output:

Warning: Illegal offset type in ....

and

var_dump($people)

output

array(0) { } 

better

foreach($people as $key => $person)
<your code>

$people[$key]["forename"] = $f_name;

I think the main problem here is that you try to use $person as index. Either you give your array ($people) a proper index or create a completely new array which you populate with your new $persons in your while-loop.

For the index solution add people to your array like this instead:

$people[$i++] = $person;

For the new array solution, create a new array and then populate it using:

$newPeople[] = $person;