russthebarber Posted August 31, 2012 Share Posted August 31, 2012 if i have arrays: $namesArray = array("bob", "jim", "alan", "mark"); $agesArray = array("44", "33", "42", "40"); How can I declare a variable using the strings stored in the namesArray as the variable name. For example: $bob = "44"; $jim = "33"; //etc... Quote Link to comment Share on other sites More sharing options...
spiderwell Posted August 31, 2012 Share Posted August 31, 2012 you can use Eval() $namesArray = array("bob", "jim", "alan", "mark"); $agesArray = array("44", "33", "42", "40"); foreach ($namesArray as $key=>$value) { eval('$' . $value . ' = ' . $agesArray[$key] . ';'); } Quote Link to comment Share on other sites More sharing options...
russthebarber Posted August 31, 2012 Author Share Posted August 31, 2012 great. Thanks, Spiderwell I have just seen that I can do $$namesArray[1]; to make a variable $jim; But I have bad feeling that there must be a catch. Are there any pitfalls or problems with using this way? Is the way you suggested robuster? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2012 Share Posted August 31, 2012 You don't need eval. PHP has "variable variables". <?php $namesArray = array("bob", "jim", "alan", "mark"); $agesArray = array("44", "33", "42", "40"); foreach ($namesArray as $key=>$value) { $$value = $agesArray[$key]; } echo '$bob = '.$bob; ?> Quote Link to comment Share on other sites More sharing options...
spiderwell Posted August 31, 2012 Share Posted August 31, 2012 the variable variables way is just as good, if not better than my method, but like many things in programming there is often more than 1 solution Quote Link to comment Share on other sites More sharing options...
russthebarber Posted August 31, 2012 Author Share Posted August 31, 2012 OK. That's a massive help. Thanks all. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 31, 2012 Share Posted August 31, 2012 You should NOT be making a series of named variable for a set of data. Having variables named like - $bob, $jim, ..., doesn't indicate the meaning of the data and it indicates that you either have some hard-coded logic later that references those variables (meaning that you must edit your code every time you change the amount of data) or that you will attempt to loop over that series of variables using the same method that you used to create them. You must also insure that the list of variables are not used anywhere else in your code so that you don't get unexplained results by overwriting variables. You should be using an array for a set of same meaning data, where the array name indicates the purpose of the data, not whom the data belongs to - $ages['bob'] = 44; $ages['jim'] = 33; Also, variable variables are 3x slower than using an array, which would have an impact if you have a large set of data. Edit: using an array will also simplify and speed up the code - $ages = array_combine($namesArray , $agesArray); Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2012 Share Posted August 31, 2012 I was trying to find the right function, I think what you really want to do is use array_combine. http://us2.php.net/manual/en/function.array-combine.php That way you can have an array like PFM describes from your current two arrays. Quote Link to comment Share on other sites More sharing options...
russthebarber Posted August 31, 2012 Author Share Posted August 31, 2012 Thanks for the tip, PFMaBiSmAd. My "bob, jim..." example was just to simplify things. But seems to have caused some confusion instead. What I am actually doing is sending a massive amount of form data to a php file and then using this method to give variables appropriate names. So I will actually have two arrays: $varsArr = array("first_name", "last_name", "town"); //etc for 100s of fields $valuesArr = array("Bob", "Johnson", "Miami"); //etc for 100s of fields My vars will then have valid names. But there is probably an even better way of doing this. Maybe a function that receives a JSON array of my form data and converts it all to vars with values? This data will be used to create a new instance of an object later. A user for example. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2012 Share Posted August 31, 2012 I'd use the array_combine method. However if you want to post the actual code, we might be able to explain what would be easiest in the long run Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 31, 2012 Share Posted August 31, 2012 What I am actually doing is sending a massive amount of form data to a php file There's absolutely no reason to break the data into individual scaler variables and if you did, you would use extract with one of the optional parameters to prevent overwriting existing variables. I have a football form that can submit 2500+ pieces of team/roster update data. I don't have one individual scaler $variable created from that data, let alone 2500 of them. Everything is handled and processed as arrays for the sets of related data. If you want a list (array) of expected form field names for validation/data access purposes, then do that (loop over the array of expected field names and access that field name in the $_POST array.) But you don't need to have individually named variables for each piece of data. Quote Link to comment Share on other sites More sharing options...
russthebarber Posted August 31, 2012 Author Share Posted August 31, 2012 OK. That all makes good sense. I'll drop the variable idea completely I think and use extract(). That looks like a brilliantly useful function. I didn't know about it before. A million thanks. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2012 Share Posted August 31, 2012 I feel like you didn't really read his entire post... Quote Link to comment Share on other sites More sharing options...
russthebarber Posted August 31, 2012 Author Share Posted August 31, 2012 Thanks Jesi. i did indeed misread the part about extract(). I am well on the way to a good solution now though. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.