Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Assign array to variables

Tags:

arrays

php

list

I'm not sure if my memory is wrong, but when I last used PHP (years ago), I vaguely remember doing something like this:

$firstVariable, $secondVariable = explode(' ', 'Foo Bar'); 

Note that the above is incorrect syntax, however in this example it would assign 'Foo' to $firstVariable, and 'Bar' to $secondVariable.

What is the correct syntax for this?

Thanks.

like image 436
TomBBB Avatar asked Jul 27 '10 04:07

TomBBB


People also ask

Can you put variables in an array PHP?

Yes, you can store variables within arrays, though you'll need to remove the space between $result and the opening bracket.

How do I assign one array to another in PHP?

Given two array arr1 and arr2 and the task is to append one array to another array. Using array_merge function: This function returns a new array after merging the two arrays. $arr1 = array ( "Geeks" , "g4g" );

What does list () do in PHP?

The list() function is used to assign values to a list of variables in one operation. Note: Prior to PHP 7.1, this function only worked on numerical arrays.


Video Answer


1 Answers

list($firstVar, $secondVar) = explode(' ', 'Foo Bar');

list() is what you are after.

like image 161
Jim Avatar answered Oct 12 '22 05:10

Jim