Jump to content

make variable from string in array


russthebarber

Recommended Posts

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;
?>           

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.