How do I name these PHP arrays?

Go To StackoverFlow.com

0

Lets say I need three arrays, ford, chevy and dodge. Each array has three items:

$ford['engine'] = 'something';
$ford['color'] = 'something';
$ford['price'] = 'something';

$chevy['engine'] = 'something';
$chevy['color'] = 'something';
$chevy['price'] = 'something';

$dodge['engine'] = 'something';
$dodge['color'] = 'something';
$dodge['price'] = 'something';

I can write that out no problem and it doesn't take too long. But, lets say I need fifty or sixty arrays that I need to make, all with ten to twenty different items. I want to put a variable at the top of each array's file to denote what the array name will be, but I am not quite clear on the syntax and I am not too familiar with $$ or how I would use that with arrays:

$name = 'ford';

$(I want this to be "$name")['engine'] = 'something';
$(I want this to be "$name")['color'] = 'something';
$(I want this to be "$name")['price'] = 'something';

I have also considered just doing this:

$name = 'ford';

$view[$name.'_engine'] 
$view[$name.'_color'] 
$view[$name.'_price'] 

Can I get some suggestions on the best way to approach this?

2012-04-03 23:41
by absentx


5

Write a small function to do that

$cars = array(); //Create an array to hold the values    
function writeArray($car, $var1, $var2, $var3) {
     global $cars;
     $cars[$car] = array('engine' =>  $var1, 'color' => $var2, 'price' => $var2);
}
//Now use it like
writeArray('Chevy', $something, $something, $something);

//If you want to access the array in the form of $ford['engine'] then use this

extract($cars); //This will split the array into small array accessible by model 
2012-04-03 23:45
by Starx
+1 for recommending that everything go into $cars. While variable variables are a fun thought experiment, they're rarely a good idea - ghoti 2012-04-03 23:53
why is that though? Why are they a bad idea - absentx 2012-04-04 00:29
@absentx, Its because, You are allowing basically an anomaly to get a simple thing to work. And besides, It doesn't give that much of a change, than the one you are already using - Starx 2012-04-04 00:31
@absentx, And I made my function shorte - Starx 2012-04-04 00:33
Downvoted for using globals. Pretty much example of how not to write functions in php - Alexei Tenitski 2012-04-04 02:30


4

You can use variable variables:

$var = 'ford';
$$var['engine'] = '...';
$$var['color'] = '...';

$var = 'chevy';
$$var['engine'] = '...';
$$var['color'] = '...';

Or just use multidimensional array:

$cars = array();

$make = 'ford';
$cars[$make] = array();
$cars[$make]['engine'] = '...';
$cars[$make]['color'] = '...';

// or

$cars['ford'] = array(
    'engine' => '...',
    'color' => '...',
);
2012-04-03 23:45
by Alexei Tenitski
This does not give that coding leverage. It shortens it , but still a lot of room - Starx 2012-04-04 00:50
How is engine and color be defined for every item in every for each loop. This leads a messy solution -1 - Starx 2012-04-04 05:33
Ads