Hi

I've been searching on how to use php variable names inside an array and to much dismay I've found nothing. I'm sure it's something really simple but I can't seem to get it even after the numerous educated guesses I've made and tried.

What I want to do is use values I pull from a database and incorporate them in a bar graph script I downloaded. The values would go in the line of code below:

$data == array(1 => 0, 1.2, 2.5, 4.8, 16, 20, 22, 17, 7, 2, 1, 0);

Instead of having numbers I'd like to have variables such as $data_01
Any help would be much appreciated.

P.S. I'll give u a Pat on the back if you can help. Pat's a really nice guy ;)

I don't think that's possible (I'm sure it's not posssible in C++ so...). But why not use classses? Or what are you trying to do?

I'd like to have a voting system on my site and be able to view the graph showing the results of the votes made from 1 through to 10.

So for example:
A user would rate something out of 10 and that vote would go into the MySQL database in the appropriate column and then a page would come up showing a graph of all votes made on that item.

This what you mean?

$data=array();
$data["foo"]=3;
$data["bar"]=4;

$three = $data["foo"];
$four = $data["bar"];

No. What he means is, he wants an array with user defined variables as index. Eg. instead of accessing the array like, $arr[0], $arr[1],.. he wants to access it like, $arr[$data_01], $arr[$data_02],...

I dont think its possible.

It's definitely possible, use the syntax nav33n used.

oh oops! :-O

<?php
$arr=array("data_01"=>1,"data_02"=>2,"data_03"=>3);
?>

I have tried a few things today based on this and it won't work unless you set the var's in the script before you pass it into an array, which kind of defeats the point(i think).

Another way you could try to do is load the data from a file and explode to populate the array, but you would have to have another script to create, populate and save said file first then it might work, but this could over complicate things i think.

Best of luck

Edit:

Naveen's cracked it methinks, i didn't try that method i tried:

$data1 = '1';
$data2 = '2';
$date3 = '3';

$array_test = array("$data1","$data2","$data3");

If i am right naveens output would allow you to call:

echo "$arr[data_01]";

etc. etc.

You must use the $ with the variable inside the brackets or it wont work echo $arr[$data_01] If you are only echoing variables then you don't need quotes.

Also $array_test = array("$data1","$data2","$data3"); You don't need quotes around the variables, in fact I think it will yell at you if you use them.

lol that explains why mine was shouting at me, cheers shawn


Naveen's cracked it methinks, i didn't try that method i tried:

$data1 = '1';
$data2 = '2';
$date3 = '3';

$array_test = array("$data1","$data2","$data3");

If i am right naveens output would allow you to call:

echo "$arr[data_01]";

etc. etc.

Before I started this thread I was trying exactly what you initially tried DaitoTsu.
This is what the array looks like untouched:

$data == array(1 => 0, 1, 2, 4, 16, 20, 22, 17, 7, 2);

Now I want the values to be variable names instead, so that I can use a MySQL query to fetch the values out of my database. The first '0' would be vote one, all the way through to the last value being vote 10.

EDIT:
The script that I have takes the array and draws a graph using the values in it.

Hi guys, I've managed to solve my problem by having the the values as variable names as below:

$data = array($vote1, $vote2, $vote3, $vote4, $vote5, $vote6, $vote7, $vote8, $vote9, $vote10);

The only thing is I had to include a file with the code requesting the values from the database. For some reason the script screams if it has any sort of code other than it's own. Haven't been able to figure out why. This isn't ideal as I'd like to have the graph placed inside other html. Including it doesn't work but I guess my initial problem has been solved.

Guys, I just had a brain fart and solved my second problem. Instead of using the file that creates the graph as a php include I just stuck it inside an image tag.

I'm having trouble putting $a and $b and $c in an array called $stored_values, assigning them to values of another array called $posted_values and echoing them on the screen!

Here's what I tried to do:

<?php

$posted_values = array(4, 5, 6);
$stored_values = array($a, $b, $c);

$i = 0;
foreach($posted_values as $posted_value) {
    $stored_values[$i]= $posted_value;
    $i++;
}

echo $a;
echo $b;
echo $c;

?>

The three echo statements do nothing. Instead 4, 5 and 6 are echoed when I say:

<?php

echo $stored_values[0];
echo $stored_values[1];
echo $stored_values[2]; 

?>

I think what happened is that $a was removed and instead the value 4 was added in the first place of the array. What I want is $a to stay and to be assigned to 4 (and the same goes to $b and $c).

I don't know if my interpretation is what really happened. So I hope someone can help me figure this out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.