Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Array destructuring

Use an assignment pattern to select fields and unpack nested arrays.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

["id" => $id, "point" => [$x, $y]] = $row;

Use an assignment pattern to select fields and unpack nested arrays. Unkeyed slots read numeric keys starting at zero rather than following arbitrary insertion order. A missing key produces E_WARNING and assigns null. A reference marker on a selected slot can bind the variable to the original element.

$row=["point"=>[4,7],"id"=>"A"]; ["id"=>$id,"point"=>[$x,&$y]]=$row; ++$y;
echo json_encode([$id,$x,$row["point"]]);
["A",4,[4,8]]

Examples

<?php
$row=["point"=>[4,7],"id"=>"A"]; ["id"=>$id,"point"=>[$x,&$y]]=$row; ++$y;
echo json_encode([$id,$x,$row["point"]]);

Result

["A",4,[4,8]]

Sources