How To Copy Array Values to a List of Variables?

deepak singh
Posts: 686
Joined: Fri Sep 30, 2016 4:19 pm

How To Copy Array Values to a List of Variables?

Postby deepak singh » Sat Oct 01, 2016 5:27 pm

How To Copy Array Values to a List of Variables?

rameshkhd
Posts: 1279
Joined: Sun Aug 28, 2016 10:45 am

Re: How To Copy Array Values to a List of Variables?

Postby rameshkhd » Sat Oct 01, 2016 5:28 pm

If you want copy all values of an array to a list of variable, you can use the list() construct on the left side of an assignment operator. list() will only take values with integer keys starting from 0. Here is a PHP script on how to use list() construct:
<?php
$array = array("Google", "Yahoo", "Netscape");
list($first, $second, $third) = $array;
print("Test 1: The third site = $third\n");
list($month, $date, $year) = split("/","1/1/2006");
print("Test 2: Year = $year\n");
$array = array("Zero"=>"PHP", 1=>"Basic", "One"=>"Perl",
0=>"Pascal", 2=>"FORTRAN", "Two"=>"Java");
list($first, $second, $third) = $array;
print("Test 3: The third language = $third\n");
?>
This script will print:
Test 1: The third site = Netscape
Test 2: Year = 2006
Test 3: The third language = FORTRAN
Test 2 uses the array returned by the split() function. Test 3 shows that list() will ignore any values with string keys.


Return to “PHP”

Who is online

Users browsing this forum: No registered users and 1 guest