Tuesday, 18 May 2021

Javascript assign Object Property to variable && PHP assign array property to varaible

 Javascript assign Object Property to variable (ES6)

var result = {data:'test'};

var {data} = result 


// console.log(data) // "test"



PHP assign array property to varaible


https://supunkavinda.blog/php-list

?php
$profile = ['James', 25, 'MIT'];

Your profile data is stuck in an indexed array ($profile). You know that each index is your name, age and the university respectively. So, now, you are going to break it into 3 variables for ease.

$name = $profile[0];
$age = $profile[1];
$uni = $profile[2];

Okay, if you had 5 variables, 5 lines. We need a better solution!

list() function language construct

list() is a language construct in PHP, which can be used for array destructuring.

Array destructuring is breaking an array into variables
<?php
list($name, $age, $uni) = $profile;

// $name = 'James'
// $age = 25
// $uni = 'MIT'

No comments:

Post a Comment