Page 1 of 1

Create dynamic array name - assign values to it

Posted: Sat Aug 09, 2014 4:31 pm
by pizzipie
Hi,

I need help to construct an array totally dynamically including the name.

I have a 'project' array $projects and a 'materials' array $materials.

$projects structure - projid, project

$materials structure - project, workcode, description, cost, tax, invno, vendor.

What I want to do is create an array for each project comprised of elements taken from the materials array. This includes creating the name,

ie: Where $project['project'] = 'house'
create an array called $house and pick all indexes and values from the $materials array having $materials['project'] named 'house'.

QUESTION How can I create the array "$house" from $myVar="$".$project['project']."[]" ($myVar would then be '$house[]')

And then assign values to it:

$myVar=$materials[$i]['description']." - ".$materials[$i]['cost']." - ".$materials[$i]['tax'] ... etc.

I've tried {$myVar}, ($myVar), "$myVar", '$myVar' etc., etc.

Thanks in advance R.

Re: Create dynamic array name - assign values to it

Posted: Sat Aug 09, 2014 4:39 pm
by Celauran
$$project['project']

Code: Select all

php > $project = [];
php > $project['project'] = 'house';
php > $$project['project'] = "This is my house.";
php > print_r($house);
This is my house.

Re: Create dynamic array name - assign values to it

Posted: Sat Aug 09, 2014 8:05 pm
by pizzipie
SOLVED:

Thanks very much celauran. Works fine.

R