Jump to content

Recommended Posts

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...

 

Link to comment
https://forums.phpfreaks.com/topic/267853-make-variable-from-string-in-array/
Share on other sites

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?

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

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

 

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.

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.

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.

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.