How to assign a list of variables from array with list() in PHP

5 Answers

0 votes
$arr = array('C', 'C++', 'PHP');

list($a, $b, $c) = $arr;
echo "$a - $b - $c";


/*
run: 

C - C++ - PHP 

*/

 



answered Jul 3, 2016 by avibootz
0 votes
$arr = array('C', 'C++', 'PHP');

list($a, $b) = $arr;
echo "$a - $b";


/*
run: 

C - C++ 

*/

 



answered Jul 3, 2016 by avibootz
0 votes
$arr = array('C', 'C++', 'PHP');

list(, , $c) = $arr;
echo "$c";


/*
run: 

PHP 

*/

 



answered Jul 3, 2016 by avibootz
0 votes
list($a, list($b, $c)) = array(100, array(200, 300));

echo "$a - $b - $c";


/*
run: 

100 - 200 - 300  

*/

 



answered Jul 3, 2016 by avibootz
0 votes
$arr = array('PHP', 'C', 'C++');

list($a[0], $a[1], $a[2]) = $arr;

echo "<pre>";
var_dump($a);
echo "</pre>";


/*
run: 

array(3) {
  [2]=>
  string(3) "C++"
  [1]=>
  string(1) "C"
  [0]=>
  string(3) "PHP"
}

*/

 



answered Jul 3, 2016 by avibootz
...