kritro Posted November 3, 2007 Share Posted November 3, 2007 I have a php script that makes multiple sql query's that results multiple datasets that I will use in a graph. Its set up something like this. for($i=0; $i<$n; $i++){ sql query runs here n times and produce each time a dataset that looks like. 2007-10-17 3 2007-10-18 4 2007-10-19 2 I want to put each dataset in an Array with diferent name. I manage to put on dataset it in one array $ydata[] = $row[1]; $xdata[] = $row[0]; } but I need one array of every dataset with names that I can recognize outside the loop, for eks a dynamic variabel name that consist of $i and some other name. like $i.$xdata[], but I cant make this work. Anyone now how to do this I would be very grateful. kritro Quote Link to comment https://forums.phpfreaks.com/topic/75957-solved-dynamic-variabel-names-in-arrays/ Share on other sites More sharing options...
Wuhtzu Posted November 3, 2007 Share Posted November 3, 2007 http://www.php.net/manual/en/language.variables.variable.php Quote Link to comment https://forums.phpfreaks.com/topic/75957-solved-dynamic-variabel-names-in-arrays/#findComment-384457 Share on other sites More sharing options...
cooldude832 Posted November 3, 2007 Share Posted November 3, 2007 its a multi deminsonal array you can name any depth array $array['var1']['var2']['var3'] = "total"; its a fundemental idea to oop like such as $user_data[$userid]['Name'] = "Joe"; $user_data[$userid]['Birthday'] = "2002/06/06"; Make sense Quote Link to comment https://forums.phpfreaks.com/topic/75957-solved-dynamic-variabel-names-in-arrays/#findComment-384470 Share on other sites More sharing options...
PHP_PhREEEk Posted November 3, 2007 Share Posted November 3, 2007 The example cooldude gave you is accurate... I'll just try to expand so you'll grasp what's going on. With arrays, there is a BIG difference between the array name (which is a variable name) and an array KEY. You are trying to create a variable name that is recognizable, but that defeats the point to using KEYS within arrays. Each added key digs one level lower into the array. Each level can either be data or another KEY. Consider the variable name for the array the 'parent'. You can have children, grandchildren, great-grandchildren, etc etc... Each of those groups of 'family' members can have separate traits (data) associated with them, but in the end, they all share the same 'family name' of the parent. Arrays are a poor man's Objects. So, instead of trying to dynamically rename each Parent, associate your data as children to that parent. Using your example, instead of $i.$xdata[], what we are looking for is $xdata[$i][] Each $i becomes a child to $xdata (the Parent or Family). The [] at the end signifies the data that each $i child will possess. $ydata[$y][] will be a second Family or Parent with its own children, and those children will have their own data (or attributes, if you will). PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/75957-solved-dynamic-variabel-names-in-arrays/#findComment-384486 Share on other sites More sharing options...
cooldude832 Posted November 3, 2007 Share Posted November 3, 2007 thats the idea, but the keys are a bit better than you think, you can sort by them, and recall from them, making things a lot more powerful Quote Link to comment https://forums.phpfreaks.com/topic/75957-solved-dynamic-variabel-names-in-arrays/#findComment-384491 Share on other sites More sharing options...
kritro Posted November 5, 2007 Author Share Posted November 5, 2007 Thank you very much. This solved my problem Quote Link to comment https://forums.phpfreaks.com/topic/75957-solved-dynamic-variabel-names-in-arrays/#findComment-385098 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.