Create dynamic array name - assign values to it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Create dynamic array name - assign values to it

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Create dynamic array name - assign values to it

Post 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.
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Create dynamic array name - assign values to it

Post by pizzipie »

SOLVED:

Thanks very much celauran. Works fine.

R
Post Reply