Basicly I have data that I want to put into variables with names that increase like so. $id = 1; $name = 'bob'; $pass = 'foo'; $id = 2; $name = 'billy'; $pass = 'bar'; What I want is PHP to loop through the vars and output: $id_1 = 1; $id_2 = 2; $name_1 = 'bob'; $name_2 = 'billy'; $pass_1 = 'foo'; $pass_2 = 'bar'; How can I do this. Like is there anyway to append variables names depending on the loop number? Thanks. ~imozeb
Why dont you do it as an array of hashes; <?php $array=array(); $array[0]['name']='bob'; $array[0]['pass']='foo'; $array[1]['name']='billy'; $array[1]['pass']='bar'; $count=0; foreach($array as $hash) { print "\$id_".$count." = ".$count."\n"; print "\$name_".$count." = '".$hash['name']."'\n"; print "\$pass_".$count." = '".$hash['pass']."'\n"; $count++; } ?> PHP: OUTPUT: $id_0 = 0 $name_0 = 'bob' $pass_0 = 'foo' $id_1 = 1 $name_1 = 'billy' $pass_1 = 'bar' Code (markup):
the $$ sign is terrible in terms of performance and code readability - use arrays in the way lukeg32 mentioned.
Okay. I've done as lukeg32 said and made an array. Now how do I store the PHP array in a javascript array so it can be used after the page has been loaded? Is it even possible?!? And how do you create a proper array? I been trying something like this: $array = array(); while($n < 5) { ++$n; $array[$n]['1'] = $var; $array[$n]['2'] = $var2; //$var 1 and 2 are dynamic }
@ThomasTwen Hello, I'm still learning PHP, so I don't understand what you mean regarding variable variables ("$$") and performance. Wouldn't the efficiency/performance be better compared to an array? Or I guess it depends on how many variable variables you have....
You need to serialize the data before passing it to javascript unless you are processing the javascript function within php. http://uk3.php.net/manual/en/function.serialize.php The above will give you; array[0][1] = $var $array[0][2] = $var2 etc If you want to know which variable should belong in which the you are better off usinjg hash keys instead; $array[$n]['YOUR_KEY'] = $var; $array[$n]['YOUR_OTHER_KEY'] = $var2; PHP:
Using variable variables ($$) is not a sufficient solution to the ops problem - certainly nothing that will beat the array example given above. PHP's array processing is actually very very fast.
You can specify the elements and keys when you create the array if you prefer, its up to you. What is the problem you are getting? is it an error or is the array empty? Dont forget that, as in your example $n is your incremental counter; you must still do this inside the while (or for... ) loop. If you arent getting any errors, paste your code in, and ill take a look.
Instead of using serialize as it's mostly for objects, try using json_encode if your PHP version supports it.
@lukeg32: I've fix the array, thanks for saying you'll help. Now all I have to do is figure out how to change it to a javascript array so I can use the values after the page has been loaded. How would I do this? Thanks.
Nonsense! Serialize is for any data structure!! (though, you are semi-correct, json_encode might also work)
No problem at all. What part are you having problems with? It would help better to understand what you are trying to achieve since there are various ways to do it; serialize is a standard way of passing a data structure from one place to another for example; Are you processing a form or such and then passing this data to an external site / entity or are you preloading the data through php to be used in a javascript function as the page loads? Do you have an example output/code of where you are having the problem?
Okay, here's what I am trying to do. I have created a function that creates an array from data from a database as: $array[$n]['name'] = $value; $array[$n]['pass'] = $value; This code is in a while loop which increments $n and changes $value. It works, because I checked the array using var_dump($array); and it returned the values I was expecting. Now what I am trying to do is take PHP $array and change it into a javascript array. I have to do this so that the array values are not lost after the page loads so that javascript can use the data to populate input fields depending on what the user selects in a dropdown input box. Basicly I need a way to transfer the PHP array into a Javascript array. Thanks! ~imozeb
Its the javascript part that doesnt make sense..... For example, there are multiple ways of processing the data. If you are passing from PHP to javascript and vice-versa then in many cases you should serialize the data..... If you have a dynamic page being created from PHP and you are preloading the javascript then you can either process it inside php with a custom routine or use json_encode. http://uk3.php.net/manual/en/function.serialize.php http://uk3.php.net/manual/en/function.json-encode.php There are of course plenty of other ways you could be doing this, hence the confusion, but you will almost certainly need one of the above.