How to use return to process the array to return the new key name?

<?php
function transform($lesson){
    return [
            "title" => $lesson["title"],
            "content" => $lesson["body"],
            "is_free" => (boolean) $lesson["free"]
        ];
}

$lesson = array("title"=>"","body"=>"","free"=>1);
var_dump(transform($lesson));
//array("title"=>"","content"=>"","is_free"=>true)
?>

how does the return in the function change the key name of the original array? I don"t understand this usage.

Php
Jul.13,2022

has not changed, but a new


has been created.

yours is to return a new array


$lesson = array('title'=>'','body'=>'','free'=>1);

//
$lesson = array('title'=>'','body'=>'','free'=>1);
$lesson['is_free'] = $lesson['free'];
$lesson['content'] = $lesson['body'];
unset($lesson['free']);
unset($lesson['body']);
var_dump($lesson);

returns a new array. The health value of the third element is is_free


.

created a new array,

$lesson1 = transform($lesson); 
$lesson = array('title'=>'','body'=>'','is_free2'=>1);
var_dump($lesson1); 
var_dump($lesson);

it doesn't matter whether you change $lesson1 or change $lesson! Unless you are passing

by reference.
$lesson1 = transform(&$lesson); 


the head is short-circuited. Thank you for your reply.


there is the correspondence of key= > val. The transform function associates the original val to the new key and returns a new result.

Menu