Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/75957-solved-dynamic-variabel-names-in-arrays/
Share on other sites

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

 

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

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.